[BACK]Return to fw_install.sh CVS log [TXT][DIR] Up to [local] / openbsd / fw_update

Diff for /openbsd/fw_update/fw_install.sh between version 1.10 and 1.25

version 1.10, 2021/10/17 03:05:29 version 1.25, 2021/11/11 02:38:32
Line 6 
Line 6 
         sed -n "$1" /var/run/dmesg.boot          sed -n "$1" /var/run/dmesg.boot
 }  }
   
 installed_firmware() {  # Fake up some things from install.sub that we don't need to actually do
         for fw in ${PKGDIR}/$1-firmware*; do  prefetcharea_fs_list() {
                 [ -e "$fw" ] || continue          echo "/mnt/tmp"
                 echo ${fw##*/}  
         done  
 }  }
   reset_watchdog() {
   }
   
 # tmpdir, do_as, unpriv, and unpriv2 are from install.sub  # tmpdir, do_as, unpriv, and unpriv2 are from install.sub
   
Line 70 
Line 70 
         do_as _file "$@"          do_as _file "$@"
 }  }
   
   # "fail" needs to be replaced with the "ask_yn" loop like in the installer.
 _issue=  _issue=
 fail() {  fail() {
         echo $_issue >&2          echo $_issue >&2
Line 80 
Line 81 
 VERSION="${VNAME%.*}${VNAME#*.}"  VERSION="${VNAME%.*}${VNAME#*.}"
 FWDIR="$VNAME"  FWDIR="$VNAME"
   
   # TODO: We need the firmware for the system we just installed
   #       not the one we booted from.  For example:
   #       * booting from a snapshot bsd.rd that thinks it is the 7.0 release
   #         will install the firmware from the 7.0 directory instead of
   #         from the snapshots dir.
   #       If they're using sysupgrade, then the installer kernel will be correct.
   #       If we're doing this in the installer we can check what they picked
   #       for downloading sets and use that value.
   #       Otherwise, the fw_update after first boot will fix it up for us.
   
 HTTP_FWDIR=$FWDIR  HTTP_FWDIR=$FWDIR
 set -- $(scan_dmesg "/^OpenBSD $VNAME\([^ ]*\).*$/s//\1/p")  set -- $(scan_dmesg "/^OpenBSD $VNAME\([^ ]*\).*$/s//\1/p")
 [[ $1 == -!(stable) ]] && HTTP_FWDIR=snapshots  [[ $1 == -!(stable) ]] && HTTP_FWDIR=snapshots
   
 FWURL=http://firmware.openbsd.org/firmware/${HTTP_FWDIR}  FWURL=http://firmware.openbsd.org/firmware/${HTTP_FWDIR}
 FWPUB_KEY=${DESTDIR}/etc/signify/openbsd-${VERSION}-fw.pub  FWPUB_KEY=${DESTDIR}/etc/signify/openbsd-${VERSION}-fw.pub
 PKGDIR=${DESTDIR}/var/db/pkg  FWPATTERNS="file:${0%/*}/firmware_patterns"
 PATTERNS="file:${0%/*}/firmware_patterns"  
   
 drivers=$(  # TODO: support srclocal installation of firmware somehow
         last=''  fw_update() {
         ftp -D "Detecting" -Vmo- $PATTERNS |          local _src=$1 _tmpfs_list _tmpfs _tmpsrc \
         while read d m; do                  _t=Get _cfile="/tmp/SHA256" _pkgdir=/mnt/var/db/pkg \
                 grep=grep                  _f _r _remove _i _installed
                 [ "$last" = "$d" ] && continue          local _srclocal=false _unpriv=unpriv
                 [ "$m" ] || m="^$d[0-9][0-9]* at "          local _d _drivers=$(
                 [ "$m" = "${m#^}" ] && grep=fgrep                  last=''
                 $grep -q "$m" /var/run/dmesg.boot || continue                  $_unpriv ftp -D "Detecting" -Vmo- $FWPATTERNS |
                 echo $d                  while read _d _m; do
                 last=$d                          grep=grep
         done                          [ "$last" = "$_d" ] && continue
 )                          [ "$_m" ] || _m="^$_d[0-9][0-9]* at "
                           [ "$_m" = "${_m#^}" ] && grep=fgrep
                           $grep -q "$_m" /var/run/dmesg.boot || continue
                           echo $_d
                           last=$_d
                   done
           )
   
 if [ -z "$drivers" ]; then          if [ -z "$_drivers" ]; then
         echo "No devices found which need firmware files to be downloaded." >&2                  echo "No devices found which need firmware files to be downloaded."
         exit 0                  return
 fi          fi
   
 _src=$FWURL          ! _tmpfs_list=$(prefetcharea_fs_list) &&
 if _tmpsrc=$( tmpdir "${DESTDIR}/tmp/fw_update" ); then                  echo "Cannot determine prefetch area" >&2 && return
         (  
         >$_tmpsrc/t &&  
         $_unpriv cat $_tmpsrc/t  
         ) >/dev/null 2>&1 ||  
             rm -r $_tmpsrc  
 fi  
   
 [[ ! -d $_tmpsrc ]] &&          for _tmpfs in $_tmpfs_list; do
         _issue="Cannot create prefetch area" && fail                  # Try to clean up from previous runs, assuming
                   # the _tmpfs selection yields the same mount
                   # point.
                   for _tmpsrc in $_tmpfs/firmware.+([0-9]).+([0-9]); do
                           [[ -d $_tmpsrc ]] && rm -r $_tmpsrc
                   done
   
 cd "$_tmpsrc"                  # Create a download directory for the firmware and
                   # check that the _sndio user can read files from
                   # it. Otherwise cleanup and skip the filesystem.
                   if _tmpsrc=$(tmpdir "$_tmpfs/firmware"); then
                           (
                           >$_tmpsrc/t &&
                           $_unpriv cat $_tmpsrc/t
                           ) >/dev/null 2>&1 && break ||
                                   rm -r $_tmpsrc
                   fi
           done
   
 _t=Get          [[ ! -d $_tmpsrc ]] &&
 _cfile="$_tmpsrc/SHA256"                  echo "Cannot create prefetch area" >&2 && return 1
 _srclocal=false  
   
 ! $_unpriv ftp -D "$_t" -Vmo - "$_src/SHA256.sig" >"$_cfile.sig" &&          # Cleanup from previous runs.
     _issue="Cannot fetch SHA256.sig" && fail          rm -f $_cfile $_cfile.sig
   
 # Verify signature file with public keys.          _t=Get/Verify
 ! unpriv -f "$_cfile" \  
     signify -Vep $FWPUB_KEY -x "$_cfile.sig" -m "$_cfile" &&  
     _issue="Signature check of SHA256.sig failed" && fail  
   
 for d in $drivers; do          ! $_unpriv ftp -D "$_t" -Vmo - "$_src/SHA256.sig" >"$_cfile.sig" &&
         _f=$( sed -n "s/.*(\($d-firmware-.*\.tgz\)).*/\1/p" "$_cfile" )              echo "Cannot fetch SHA256.sig" >&2 && return 1
         installed=$( installed_firmware "$d" )  
   
         for i in $installed; do          # Verify signature file with public keys.
                 if [ "$_f" = "$i.tgz" ]; then          ! $_unpriv -f "$_cfile" \
                         echo "Firmware for $d already installed ($installed)"              signify -Vep $FWPUB_KEY -x "$_cfile.sig" -m "$_cfile" &&
                         continue 2              echo "Signature check of SHA256.sig failed" >&2 && return 1
                 fi  
         done  
   
         rm -f /tmp/h /tmp/fail          for _d in $_drivers; do
                   $UU && reset_watchdog
                   _f=$( sed -n "s/.*(\($_d-firmware-.*\.tgz\)).*/\1/p" "$_cfile" )
                   _installed=$(
                   for fw in "${_pkgdir}/$_d-firmware"*; do
                           [ -e "$fw" ] || continue
                           echo ${fw##*/}
                   done
                   )
   
         _t=Get/Verify                  for _i in $_installed; do
         # Fetch firmware file and create a checksum by piping through                          if [ "$_f" = "$_i.tgz" ]; then
         # sha256. Create a flag file in case ftp failed. Firmware                                  echo "$_i already installed"
         # from net is written to the prefetch area.                                  continue 2
         ( $_unpriv ftp -D "$_t" -Vmo - "$_src/$_f" || >/tmp/fail ) |                          fi
         ( $_srclocal && unpriv2 sha256 -b >/tmp/h ||                  done
             unpriv2 -f /tmp/h sha256 -bph /tmp/h >"$_tmpsrc/$_f" )  
   
         # Handle failed transfer.                  rm -f /tmp/h /tmp/fail
         if [[ -f /tmp/fail ]]; then  
                 rm -f "$_tmpsrc/$_f"  
                 _issue="Fetching of $_f failed!"  
                 fail  
         fi  
   
         # Verify firmware by comparing its checksum with SHA256.                  # Fetch firmware file and create a checksum by piping through
         if fgrep -qx "SHA256 ($_f) = $(</tmp/h)" "$_cfile"; then                  # sha256. Create a flag file in case ftp failed. Firmware
                 #_unver=$(rmel $_f $_unver)                  # from net is written to the prefetch area.
                 true                  ( $_unpriv ftp -D "$_t" -Vmo - "$_src/$_f" || >/tmp/fail ) |
         else                  ( $_srclocal && unpriv2 sha256 -b >/tmp/h ||
                 [[ -d "$_tmpsrc" ]] && rm -rf "$_tmpsrc"                      unpriv2 -f /tmp/h sha256 -bph /tmp/h >"$_tmpsrc/$_f" )
                 _issue="Checksum test for $_f failed."  
                 fail  
         fi  
   
         # TODO: Check hash for files before deleting                  # Handle failed transfer.
         if [ "$installed" ] && [ -e "${PKGDIR}/$installed/+CONTENTS" ]; then                  if [[ -f /tmp/fail ]]; then
                 echo "Uninstalling $installed"                          rm -f "$_tmpsrc/$_f"
                 cwd=${PKGDIR}/$installed                          echo "Fetching of $_f failed!" >&2
                           continue
                   fi
   
                 set -A _remove -- "${cwd}/+CONTENTS" "${cwd}"                  # Verify firmware by comparing its checksum with SHA256.
                   if ! fgrep -qx "SHA256 ($_f) = $(</tmp/h)" "$_cfile"; then
                           [[ -d "$_tmpsrc" ]] && rm -rf "$_tmpsrc"
                           echo "Checksum test for $_f failed." >&2
                           continue
                   fi
   
                 while read c g; do                  # TODO: Check hash for files before deleting
                         case $c in                  if [ "$_installed" ] && [ -e "${_pkgdir}/$_installed/+CONTENTS" ]; then
                         @cwd) cwd=$g                          echo "Uninstalling $_installed"
                           ;;                          cwd=${_pkgdir}/$_installed
                         @*) continue  
                           ;;  
                         *)  set -A _remove -- "$cwd/$c" "${_remove[@]}"  
                           ;;  
                         esac  
                 done < "${PKGDIR}/$installed/+CONTENTS"  
   
                 for _r in "${_remove[@]}"; do                          set -A _remove -- "${cwd}/+CONTENTS" "${cwd}"
                         if [ -d "$_r" ]; then  
                                 # Try hard not to actually remove recursively  
                                 # without rmdir on the install media.  
                                 [ "$_r/*" = $( echo "$_r"/* ) ] && rm -rf "$_r"  
                         else  
                                 rm -f "$_r"  
                         fi  
                 done  
         fi  
   
         # TODO: Add some details about the install to +CONTENTS like pkg_add                          while read c g; do
         # TODO: Or, maybe we save the firmware someplace and make pkg_add reinstall                                  case $c in
         echo "Installing $_f"                                  @cwd) cwd="/mnt/$g"
         tar -zxphf "$_f" -C /etc "firmware/*"                                    ;;
         mkdir -p ${PKGDIR}/${_f%.tgz}/                                  @*) continue
         tar -zxphf "$_f" -C "${PKGDIR}/${_f%.tgz}" "+*"                                    ;;
         ed -s "${PKGDIR}/${_f%.tgz}/+CONTENTS" <<EOL                                  *)  set -A _remove -- "$cwd/$c" "${_remove[@]}"
                                     ;;
                                   esac
                           done < "${_pkgdir}/$_installed/+CONTENTS"
   
                           # We specifically rm -f here because not removing files/dirs
                           # is probably not worth failing over.
                           for _r in "${_remove[@]}" ; do
                                   if [ -d "$_r" ]; then
                                           # Try hard not to actually remove recursively
                                           # without rmdir on the install media.
                                           [ "$_r/*" = $( echo "$_r"/* ) ] && rm -rf "$_r"
                                   else
                                           rm -f "$_r"
                                   fi
                           done
                   fi
   
                   # TODO: Should we mark these so real fw_update can -Drepair?
                   ftp -D "Install" -Vmo- "file:$_tmpsrc/$_f" |
                           tar -s ",^\+,${_pkgdir}/${_f%.tgz}/+," \
                           -s ",^firmware,mnt/etc/firmware," \
                           -C / -zxphf - "+*" "firmware/*"
   
                   ed -s "${_pkgdir}/${_f%.tgz}/+CONTENTS" <<EOL
 /^@comment pkgpath/ -1a  /^@comment pkgpath/ -1a
 @option manual-installation  @option manual-installation
 @option firmware  @option firmware
Line 215 
Line 248 
 .  .
 w  w
 EOL  EOL
 done          done
   }
   
   fw_update "$FWURL"

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.25

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>