]> git.ozlabs.org Git - yaboot.git/blobdiff - ybin/ybin
The attached patch adds support for writing the nvram using the nvram
[yaboot.git] / ybin / ybin
index 6b5d4c8304cc844f618ca4145dc157a8b7983b6a..19ee2aab580188abb50821a02dccd06d84b9d83f 100755 (executable)
--- a/ybin/ybin
+++ b/ybin/ybin
@@ -3,7 +3,7 @@
 ###############################################################################
 ##
 ## ybin (YaBoot INstaller) installs/updates the yaboot bootloader.
-## Copyright (C) 2000, 2001, 2002 Ethan Benson
+## Copyright (C) 2000, 2001, 2002, 2003 Ethan Benson
 ##
 ## This program is free software; you can redistribute it and/or
 ## modify it under the terms of the GNU General Public License
@@ -27,13 +27,21 @@ if [ -n "$PATH_PREFIX" ] ; then
     PATH="${PATH}:${PATH_PREFIX}/sbin:${PATH_PREFIX}/bin:${PATH_PREFIX}/usr/sbin:${PATH_PREFIX}/usr/bin:${PATH_PREFIX}/usr/local/sbin:${PATH_PREFIX}/usr/local/bin"
 fi
 PRG="${0##*/}"
+ABSPRG="$0"
 SIGINT="$PRG: Interrupt caught ... exiting"
-VERSION=1.3.7
+VERSION=1.3.14
 DEBUG=0
 VERBOSE=0
 TMP="${TMPDIR:-/tmp}"
 export LC_COLLATE=C
 
+## avoid older versions of ofpath shipped in debian boot-floppies etc.
+if [ -x "${PATH_PREFIX}/usr/sbin/ofpath" ] ; then
+    OFPATH="${PATH_PREFIX}/usr/sbin/ofpath"
+else
+    OFPATH=ofpath
+fi
+
 ## catch signals, clean up junk in /tmp.
 trap "cleanup" 0
 trap "exit 129" 1
@@ -55,7 +63,7 @@ fi
 ## define default configuration
 boot=unconfigured
 
-## allow default to work on packaged and non-packaged yaboot. 
+## allow default to work on packaged and non-packaged yaboot.
 ## no default for magicboot since it is not required everywhere.
 if [ -f /usr/local/lib/yaboot/yaboot ] ; then
     install=/usr/local/lib/yaboot/yaboot
@@ -116,7 +124,7 @@ fi
 
 ## make fake `id' if its missing, outputs 0 since if its missing we
 ## are probably running on boot floppies and thus are root.
-if (command -v id > /dev/null 2>&1) ; then 
+if (command -v id > /dev/null 2>&1) ; then
     true
 else
     id()
@@ -132,7 +140,7 @@ echo \
 "$PRG $VERSION
 Written by Ethan Benson
 
-Copyright (C) 2000, 2001, 2002 Ethan Benson
+Copyright (C) 2000, 2001, 2002, 2003 Ethan Benson
 This is free software; see the source for copying conditions.  There is NO
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
 }
@@ -147,6 +155,8 @@ Update/install bootloader onto a bootstrap partition.
   -b, --boot                 set bootstrap partition device [ -b /dev/hda2 ]
   -o, --ofboot               set bootstrap partition OpenFirmware device
                               default: automatically determined [ -o hd:2 ]
+       --bootonce            override default boot label for ONLY next boot
+                                eg: --bootonce linux-2.6.20-testing
   -i, --install              pathname to the actual bootloader binary
                                default: /usr/{local/}lib/yaboot/yaboot same as
                                install= in config file [ -i bootloader_file ]
@@ -292,7 +302,7 @@ checkconf()
            fi
            local CONFERR=1
        fi
-       
+
        if [ `echo ${#hfscreator}` != 4 ] ; then
            if [ "$ARGCT" = 1 ] ; then
                echo 1>&2 "$PRG: --creator must be 4 characters"
@@ -378,7 +388,7 @@ checkconf()
        fi
 
        ## convert defaultos variable
-       case "$defaultos" in 
+       case "$defaultos" in
            linux|Linux|GNU|Gnu|gnu)
                defaultos=bootyaboot
                ;;
@@ -426,21 +436,22 @@ checkconf()
     fi
 
     if [ "$nonvram" = 0 ] ; then
-       ## see if nvsetenv exists and is executable
-       if (command -v nvsetenv > /dev/null 2>&1) ; then
-           [ -x `command -v nvsetenv` ] || MISSING=1 ; else MISSING=1
+               ## see if nvsetenv or nvram exists and is executable
+               for program in nvsetenv nvram ; do
+                       if (command -v $program > /dev/null 2>&1) && [ -x `command -v $program` ] ; then
+                               NVRAM_PROG=$program
+                               break
+                       fi
+               done
+               if [ -z "$NVRAM_PROG" ] ; then
+                       nonvram=1
+                       echo 1>&2 "$PRG: Warning: \`nvsetenv' and \`nvram' could not be found, nvram will not be updated"
+               fi
        fi
 
        if [ "$nonvram" = 0 ] ; then
            ## if nvsetenv exists see if its the old broken version
-           if [ "$MISSING" != 1 ] ; then
-               nvsetenv --version > /dev/null 2>&1 || OLD=1
-           else
-               nonvram=1
-               echo 1>&2 "$PRG: Warning: \`nvsetenv' could not be found, nvram will not be updated"
-           fi
-
-           if [ "$OLD" = 1 ] ; then
+               if [ "$NVRAM_PROG" = "nvsetenv" ] && ! nvsetenv --version > /dev/null 2>&1 ; then
                ## i check this myself to avoid misleading error
                ## messages. nvsetenv should REALLY support --version.
                if [ ! -e /dev/nvram ] ; then
@@ -455,6 +466,10 @@ checkconf()
                    echo 1>&2 "$PRG: /dev/nvram: Permission denied"
                    echo 1>&2 "$PRG: Warning: nvram will not be updated"
                    nonvram=1
+               elif ! (dd if=/dev/nvram of=/dev/null bs=1 count=10 > /dev/null 2>&1) ; then
+                   echo 1>&2 "$PRG: /dev/nvram: No such device"
+                   echo 1>&2 "$PRG: Warning: nvram will not be updated"
+                   nonvram=1
                else
                    nonvram=1
                    echo 1>&2 "$PRG: Warning: Incompatible version of \`nvsetenv', nvram will not be updated"
@@ -544,7 +559,7 @@ if (command -v readlink > /dev/null 2>&1) ; then
 else
     readlink()
     {
-       SYMTARGET="$(v=`ls -l "$2" 2>/dev/null` ; echo ${v##*> })"
+       local SYMTARGET="$(v=`ls -l "$2" 2>/dev/null` ; echo ${v##*> })"
        if [ -n "$SYMTARGET" ] ; then
            echo "$SYMTARGET"
            return 0
@@ -600,7 +615,7 @@ convertpath()
     ## figure out bootstrap device OF pathname if user did not supply it.
     if [ -z "$ofboot" ] ; then
        [ "$VERBOSE" = 1 ] && echo "$PRG: Finding OpenFirmware device path to \`$boot'..."
-       ofboot="$(ofpath $boot)"
+       ofboot="$($OFPATH $boot)"
        if [ $? != 0 ] ; then
            echo 1>&2 "$PRG: Unable to find OpenFirmware path for boot=$boot"
            echo 1>&2 "$PRG: Please add ofboot=<path> where <path> is the OpenFirmware path to $boot to $CONF"
@@ -615,7 +630,7 @@ convertpath()
            /dev/*)
                [ "$VERBOSE" = 1 ] && echo "$PRG: Finding OpenFirmware device path to \`$bsd'..."
                local sbsd="$bsd"
-               bsd="$(ofpath $bsd)"
+               bsd="$($OFPATH $bsd)"
                if [ $? != 0 ] ; then
                    echo 1>&2 "$PRG: Unable to determine OpenFirmware path for bsd=$sbsd"
                    echo 1>&2 "$PRG: Try specifying the real OpenFirmware path for bsd=$sbsd in $CONF"
@@ -634,7 +649,7 @@ convertpath()
            /dev/*)
                [ "$VERBOSE" = 1 ] && echo "$PRG: Finding OpenFirmware device path to \`$macos'..."
                local smacos="$macos"
-               macos="$(ofpath $macos)"
+               macos="$($OFPATH $macos)"
                if [ $? != 0 ] ; then
                    echo 1>&2 "$PRG: Unable to determine OpenFirmware path for macos=$smacos"
                    echo 1>&2 "$PRG: Try specifying the real OpenFirmware path for macos=$smacos in $CONF"
@@ -653,7 +668,7 @@ convertpath()
            /dev/*)
                [ "$VERBOSE" = 1 ] && echo "$PRG: Finding OpenFirmware device path to \`$macosx'..."
                local smacosx="$macosx"
-               macosx="$(ofpath $macosx)"
+               macosx="$($OFPATH $macosx)"
                if [ $? != 0 ] ; then
                    echo 1>&2 "$PRG: Unable to determine OpenFirmware path for macosx=$smacosx"
                    echo 1>&2 "$PRG: Try specifying the real OpenFirmware path for macosx=$smacosx in $CONF"
@@ -672,7 +687,7 @@ convertpath()
            /dev/*)
                [ "$VERBOSE" = 1 ] && echo "$PRG: Finding OpenFirmware device path to \`$darwin'..."
                local sdarwin="$darwin"
-               darwin="$(ofpath $darwin)"
+               darwin="$($OFPATH $darwin)"
                if [ $? != 0 ] ; then
                    echo 1>&2 "$PRG: Unable to determine OpenFirmware path for darwin=$sdarwin"
                    echo 1>&2 "$PRG: Try specifying the real OpenFirmware path for darwin=$sdarwin in $CONF"
@@ -716,7 +731,7 @@ checkhfsutils()
 
 ## This is gross, IBM CHRP OF needs a .note added to the yaboot
 ## binary, nobody knows whether this note will affect PowerMac OF or
-## not (or could in the future). 
+## not (or could in the future).
 hack_yaboot()
 {
     local YBDIR="${install%/*}"
@@ -778,7 +793,7 @@ util_install()
 
     ## repoint magicboot as the real first stage loader if using the
     ## modern automatic generating ofboot.b.
-    if [ -n "$FIRST" ] ; then  
+    if [ -n "$FIRST" ] ; then
        magicboot="$FIRST"
        [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: set magicboot to $FIRST"
     fi
@@ -792,7 +807,7 @@ util_install()
        if [ "$protect" = yes ] ; then
            local LOCK="+l"
        fi
-       
+
        if [ "$hide" = yes ] ; then
            local INVISIBLE="+i"
        fi
@@ -912,7 +927,11 @@ util_install()
        if [ "$nonvram" = 0 ] ; then
            [ "$VERBOSE" = 1 ] && echo "$PRG: Updating OpenFirmware boot-device variable in nvram..."
            [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: boot-device=${ofboot},${OFFILE}"
-           nvsetenv boot-device "${ofboot},${OFFILE}"
+               if [ "$NVRAM_PROG" = "nvsetenv" ] ; then
+                       nvsetenv boot-device "${ofboot},${OFFILE}"
+               else
+                       nvram -p common --update-config boot-device="${ofboot},${OFFILE}"
+               fi
            if [ $? != 0 ] ; then
                echo 1>&2 "$PRG: An error occured while updating nvram, we'll ignore it"
            fi
@@ -996,7 +1015,7 @@ umnt()
 mnt_install()
 {
     local BTFILE=yaboot
-  
+
     ## msdosfs is broken, yaboot may not support this filename.
     if [ "$fstype" = msdos ] ; then
        local CFFILE=yaboot.cnf
@@ -1098,7 +1117,11 @@ mnt_install()
     if [ "$nonvram" = 0 ] ; then
        [ "$VERBOSE" = 1 ] && echo "$PRG: Updating OpenFirmware boot-device variable in nvram..."
        [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: boot-device=${ofboot},${OFFILE}"
-       nvsetenv boot-device "${ofboot},${OFFILE}"
+       if [ "$NVRAM_PROG" = "nvsetenv" ] ; then
+               nvsetenv boot-device "${ofboot},${OFFILE}"
+       else
+               nvram -p common --update-config boot-device="${ofboot},${OFFILE}"
+       fi
        if [ $? != 0 ] ; then
            echo 1>&2 "$PRG: An error occured while updating nvram, we'll ignore it"
        fi
@@ -1110,7 +1133,7 @@ mnt_install()
 }
 
 ## raw installation, for IBM RS/6000 hardware, yaboot is dded to the
-## bootstrap partition. 
+## bootstrap partition.
 raw_install()
 {
     ## make sure the device is not mounted as a filesystem before
@@ -1232,7 +1255,7 @@ mkoffs()
            [ "$VERBOSE" = 1 ] && echo "$PRG: Creating DOS filesystem on $boot..."
             if (command -v dd > /dev/null 2>&1) ; then
                 dd if=/dev/zero of="$boot" bs=512 count=1600 > /dev/null 2>&1
-            fi  
+            fi
            mkdosfs -n bootstrap "$boot" > /dev/null
             if [ $? != 0 ] ; then
                echo 1>&2 "$PRG: DOS filesystem creation failed!"
@@ -1314,7 +1337,7 @@ cleanup()
 ## absurdly bloated case statement to parse command line options.
 if [ $# != 0 ] ; then
     while true ; do
-       case "$1" in 
+       case "$1" in
            -V|--version)
                version
                exit 0
@@ -1325,19 +1348,26 @@ if [ $# != 0 ] ; then
                ;;
            --debug)
                DEBUG=1
+               ARGS="$ARGS $1"
                shift
                ;;
            -v|--verbose)
                VERBOSE=1
+               ARGS="$ARGS $1"
                shift
                ;;
            -f|--force)
                FORCE=yes
+               ARGS="$ARGS $1"
                shift
                ;;
            -b|--boot)
                if [ -n "$2" ] ; then
-                   boot="$2"
+                   if [ "$boot" = "unconfigured" ]; then
+                       boot="$2"
+                   else
+                       boot="$boot $2"
+                   fi
                    ARGBT=1
                    shift 2
                else
@@ -1350,6 +1380,18 @@ if [ $# != 0 ] ; then
                if [ -n "$2" ] ; then
                    ofboot="$2"
                    ARGOB=1
+                   ARGS="$ARGS $1 $2"
+                   shift 2
+               else
+                   echo 1>&2 "$PRG: option requires an argument $1"
+                   echo 1>&2 "Try \`$PRG --help' for more information."
+                   exit 1
+               fi
+               ;;
+           --bootonce)
+               if [ -n "$2" ] ; then
+                   bootonce="$2"
+                   ARGS="$ARGS $1 $2"
                    shift 2
                else
                    echo 1>&2 "$PRG: option requires an argument $1"
@@ -1361,6 +1403,7 @@ if [ $# != 0 ] ; then
                if [ -n "$2" ] ; then
                    install="$2"
                    ARGBF=1
+                   ARGS="$ARGS $1 $2"
                    shift 2
                else
                    echo 1>&2 "$PRG: option requires an argument $1"
@@ -1373,6 +1416,7 @@ if [ $# != 0 ] ; then
                    CONF="$2"
                    bootconf="$2"
                    ERR=" Error in $CONF:"
+                   ARGS="$ARGS $1 $2"
                    shift 2
                else
                    echo 1>&2 "$PRG: option requires an argument $1"
@@ -1384,6 +1428,7 @@ if [ $# != 0 ] ; then
                if [ -n "$2" ] ; then
                    magicboot="$2"
                    ARGWP=1
+                   ARGS="$ARGS $1 $2"
                    shift 2
                else
                    echo 1>&2 "$PRG: option requires an argument $1"
@@ -1395,6 +1440,7 @@ if [ $# != 0 ] ; then
                if [ -n "$2" ] ; then
                    fstype="$2"
                    ARGFS=1
+                   ARGS="$ARGS $1 $2"
                    shift 2
                else
                    echo 1>&2 "$PRG: option requires an argument $1"
@@ -1405,26 +1451,31 @@ if [ $# != 0 ] ; then
            --nobless)
                bless=no
                ARGBS=1
+               ARGS="$ARGS $1"
                shift
                ;;
            -M|--mount)
                usemount=yes
                ARGMT=1
+               ARGS="$ARGS $1"
                shift
                ;;
            --protect)
                protect=yes
                ARGPT=1
+               ARGS="$ARGS $1"
                shift
                ;;
            --hide)
                hide=yes
                ARGHD=1
+               ARGS="$ARGS $1"
                shift
                ;;
            --nonvram)
                nonvram=1
                ARGNV=1
+               ARGS="$ARGS $1"
                shift
                ;;
            --device)
@@ -1432,6 +1483,7 @@ if [ $# != 0 ] ; then
                    device="$2"
                    bootconf=auto
                    echo 1>&2 "$PRG: WARNING: Deprecated option --device"
+                   ARGS="$ARGS $1 $2"
                    shift 2
                else
                    echo 1>&2 "$PRG: option requires an argument $1"
@@ -1444,6 +1496,7 @@ if [ $# != 0 ] ; then
                    timeout="$2"
                    bootconf=auto
                    echo 1>&2 "$PRG: WARNING: Deprecated option --device"
+                   ARGS="$ARGS $1 $2"
                    shift 2
                else
                    echo 1>&2 "$PRG: option requires an argument $1"
@@ -1456,6 +1509,7 @@ if [ $# != 0 ] ; then
                    image="$2"
                    bootconf=auto
                    echo 1>&2 "$PRG: WARNING: Deprecated option --device"
+                   ARGS="$ARGS $1 $2"
                    shift 2
                else
                    echo 1>&2 "$PRG: option requires an argument $1"
@@ -1468,6 +1522,7 @@ if [ $# != 0 ] ; then
                    label="$2"
                    bootconf=auto
                    echo 1>&2 "$PRG: WARNING: Deprecated option --device"
+                   ARGS="$ARGS $1 $2"
                    shift 2
                else
                    echo 1>&2 "$PRG: option requires an argument $1"
@@ -1480,6 +1535,7 @@ if [ $# != 0 ] ; then
                    partition="$2"
                    bootconf=auto
                    echo 1>&2 "$PRG: WARNING: Deprecated option --device"
+                   ARGS="$ARGS $1 $2"
                    shift 2
                else
                    echo 1>&2 "$PRG: option requires an argument $1"
@@ -1492,6 +1548,7 @@ if [ $# != 0 ] ; then
                    root="$2"
                    bootconf=auto
                    echo 1>&2 "$PRG: WARNING: Deprecated option --device"
+                   ARGS="$ARGS $1 $2"
                    shift 2
                else
                    echo 1>&2 "$PRG: option requires an argument $1"
@@ -1571,6 +1628,20 @@ fi
 [ $(parseconf flag enableofboot) = 0 ] && of=yes
 [ $(parseconf flag brokenosx) = 0 ] && brokenosx=yes
 
+bootparts=0
+for i in $boot; do
+    bootparts=$(($bootparts + 1))
+done
+if [ "$bootparts" -gt 1 ]; then
+    [ "$VERBOSE" = 1 ] && echo "$PRG: Iterating through list of boot partitions..."
+    rc=0
+    for i in $boot; do
+        [ "$VERBOSE" = 1 ] && echo "$ABSPRG $ARGS -b $i"
+        $ABSPRG $ARGS -b $i || rc=$?
+    done
+    exit $rc
+fi
+
 ## ffs!! rtfm! foad!
 if [ "$boot" = unconfigured ] ; then
     echo 1>&2 "$PRG: You must specify the device for the bootstrap partition. (ie: boot=/dev/hdaX)"
@@ -1594,6 +1665,28 @@ fi
 ## validate configuration for sanity.
 checkconf || exit 1
 
+if [ "x$bootonce" != "x" ]; then
+    foundlabel=`sed s/\#.*// $bootconf | grep "label=$bootonce$" | wc -l`
+    if [ "$nonvram" = 0 ]; then
+       echo 1>&2 "$PRG: --bootonce specified, but nvsetenv not available."
+       exit 1
+    fi
+    if [ "$foundlabel" = 1 ]; then
+       nvsetenv boot-once "$bootonce"
+       foundlabel=`nvsetenv boot-once`
+       if [ "$foundlabel" != "boot-once=$bootonce" ]; then
+           echo 1>&2 "$PRG: Could not nvsetenv boot-once $bootonce"
+           exit 1
+       fi
+       [ "$VERBOSE" = 1 ] && echo "$PRG: nvsetenv boot-once $bootonce"
+    else
+       echo 1>&2 "$PRG: Could not find bootonce label [$bootonce] in $bootconf"
+       exit 1
+    fi
+fi
+
+bootparts=0
+
 ## check that we can use ofpath, its only needed for magicboot script
 ## building and nvram updates.
 if [ -n "$magicboot" -o "$nonvram" = 0 ] ; then