]> git.ozlabs.org Git - yaboot.git/blob - ybin/ybin
Version 1.3.15
[yaboot.git] / ybin / ybin
1 #! /bin/sh
2
3 ###############################################################################
4 ##
5 ## ybin (YaBoot INstaller) installs/updates the yaboot bootloader.
6 ## Copyright (C) 2000, 2001, 2002, 2003 Ethan Benson
7 ##
8 ## This program is free software; you can redistribute it and/or
9 ## modify it under the terms of the GNU General Public License
10 ## as published by the Free Software Foundation; either version 2
11 ## of the License, or (at your option) any later version.
12 ##
13 ## This program is distributed in the hope that it will be useful,
14 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ## GNU General Public License for more details.
17 ##
18 ## You should have received a copy of the GNU General Public License
19 ## along with this program; if not, write to the Free Software
20 ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 ##
22 ###############################################################################
23
24 PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
25 ## allow to run out of /target in boot-floppies
26 if [ -n "$PATH_PREFIX" ] ; then
27     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"
28 fi
29 PRG="${0##*/}"
30 ABSPRG="$0"
31 SIGINT="$PRG: Interrupt caught ... exiting"
32 VERSION=1.3.15
33 DEBUG=0
34 VERBOSE=0
35 TMP="${TMPDIR:-/tmp}"
36 export LC_COLLATE=C
37
38 ## avoid older versions of ofpath shipped in debian boot-floppies etc.
39 if [ -x "${PATH_PREFIX}/usr/sbin/ofpath" ] ; then
40     OFPATH="${PATH_PREFIX}/usr/sbin/ofpath"
41 else
42     OFPATH=ofpath
43 fi
44
45 ## catch signals, clean up junk in /tmp.
46 trap "cleanup" 0
47 trap "exit 129" 1
48 trap "echo 1>&2 $SIGINT ; exit 130" 2
49 trap "exit 131" 3
50 trap "exit 143" 15
51
52 ## allow for non-existent config file, in which case it will be
53 ## generated from command line arguments.
54 if [ -f /etc/yaboot.conf ] ; then
55     CONF=/etc/yaboot.conf
56     bootconf=$CONF
57     ERR=" Error in $CONF:"
58 else
59     CONF=/dev/null
60     bootconf=/dev/null
61 fi
62
63 ## define default configuration
64 boot=unconfigured
65
66 ## allow default to work on packaged and non-packaged yaboot.
67 ## no default for magicboot since it is not required everywhere.
68 if [ -f /usr/local/lib/yaboot/yaboot ] ; then
69     install=/usr/local/lib/yaboot/yaboot
70 elif [ -f /usr/lib/yaboot/yaboot ] ; then
71     install=/usr/lib/yaboot/yaboot
72 fi
73
74 ## defaults
75 usemount=no
76 if (cat /proc/cpuinfo 2> /dev/null | grep ^machine | grep -q 'CHRP IBM') ; then
77     fstype=raw
78 else
79     fstype=hfs
80 fi
81 hfstype=tbxi
82 hfscreator=UNIX
83 bless=yes
84 protect=no
85 hide=no
86 nonvram=0
87 defaultos=linux
88 brokenosx=no
89 cdrom=no
90 network=no
91 of=no
92 fgcolor=white
93 bgcolor=black
94
95 ## yaboot autoconf defaults
96 label=Linux
97 timeout=40
98 image=/vmlinux
99 partition=3
100 root=/dev/hda3
101 device=hd:
102
103 ## this program behaves differently based on how its called, this
104 ## ensures that nothing nasty happens if someone makes a bogus
105 ## symlink.
106 case "$PRG" in
107     ybin)
108     ;;
109     mkofboot)
110     ;;
111     *)
112     echo 1>&2 "This program must be called as either \`ybin' or \`mkofboot'"
113     exit 1
114     ;;
115 esac
116
117 ## check for printf, use it if possible otherwise fall back on
118 ## unreliable echo -e -n ("SUS" says echo shall support no switches)
119 if [ "$(printf printf_test 2>/dev/null)" = printf_test ] ; then
120     PRINTF=printf
121 else
122     PRINTF="echo -e -n"
123 fi
124
125 ## make fake `id' if its missing, outputs 0 since if its missing we
126 ## are probably running on boot floppies and thus are root.
127 if (command -v id > /dev/null 2>&1) ; then
128     true
129 else
130     id()
131     {
132     echo 0
133     }
134 fi
135
136 ## --version output
137 version()
138 {
139 echo \
140 "$PRG $VERSION
141 Written by Ethan Benson
142
143 Copyright (C) 2000, 2001, 2002, 2003 Ethan Benson
144 This is free software; see the source for copying conditions.  There is NO
145 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
146 }
147
148 ## --help output.
149 usage()
150 {
151 echo \
152 "Usage: $PRG [OPTION]...
153 Update/install bootloader onto a bootstrap partition.
154
155   -b, --boot                 set bootstrap partition device [ -b /dev/hda2 ]
156   -o, --ofboot               set bootstrap partition OpenFirmware device
157                                default: automatically determined [ -o hd:2 ]
158        --bootonce            override default boot label for ONLY next boot
159                                 eg: --bootonce linux-2.6.20-testing
160   -i, --install              pathname to the actual bootloader binary
161                                default: /usr/{local/}lib/yaboot/yaboot same as
162                                install= in config file [ -i bootloader_file ]
163   -C, --config               use alternate configuration file (ybin and yaboot)
164                                [ -C config_file ]
165   -m, --magicboot            pathname to a OpenFirmware magicboot (CHRP) script
166       --filesystem           set the filesystem type of the bootstrap partition
167                                available options are hfs, msdos, and raw
168                                [ --filesystem hfs ] default is hfs
169       --nobless              don't bless the root directory, this should only
170                                be used if you are using a MacOS boot partition
171                                as the bootstrap partition (not recommended)
172   -M, --mount                don't use userspace hfsutils to modify the
173                                bootstrap instead try and mount the filesystem
174                                directly.  Note that attributes cannot be set
175                                this way and you will have to manually modify
176                                OpenFirmware to make your system bootable
177       --protect              set the read-only (locked) bit on all bootstrap
178                                files
179       --hide                 set the invisible bit on all bootstrap files
180                                this is useful of you don't want them to be
181                                visible from MacOS.
182       --nonvram              do not update the boot-device variable in nvram.
183       --force                don't ever ask for confirmation
184   -v, --verbose              make $PRG more verbose
185       --debug                print boring junk only useful for debugging
186   -h, --help                 display this help and exit
187   -V, --version              output version information and exit"
188 }
189
190 ## configuration file parsing. FIXME: need a method which can parse
191 ## image= sections.
192 parseconf()
193 {
194 case "$1" in
195     str)
196        v=`grep "^$2[\ ,=]" "$CONF"` ; echo "${v#*=}"
197        ;;
198     flag)
199        grep "^$2\>" "$CONF" > /dev/null && echo 0 || echo 1
200        ;;
201     ck)
202        grep "^$2[\ ,=]" "$CONF" > /dev/null && echo 0 || echo 1
203        ;;
204 esac
205 }
206
207 ## check for existence of a configuration file, and make sure we have
208 ## read permission.
209 confexist()
210 {
211     if [ ! -e "$CONF" ] ; then
212         echo 1>&2 "$PRG: $CONF: No such file or directory"
213         return 1
214     elif [ ! -f "$CONF" ] ; then
215         echo 1>&2 "$PRG: $CONF: Not a regular file"
216         return 1
217     elif [ ! -r "$CONF" ] ; then
218         echo 1>&2 "$PRG: $CONF: Permission denied"
219         return 1
220     else
221         return 0
222     fi
223 }
224
225 ## check to make sure the configuration file is sane and correct.
226 ## maybe this is an insane ammount of error checking, but I want to
227 ## make sure (hopfully) nothing unexpected ever happens.  and i just
228 ## like useful errors from programs.  every error just marks an error
229 ## variable so we give the user as much info as possible before we
230 ## abandon ship.
231 checkconf()
232 {
233     if [ ! -e "$boot" ] ; then
234         echo 1>&2 "$PRG: $boot: No such file or directory"
235         local CONFERR=1
236     elif [ ! -b "$boot" -a ! -f "$boot" ] ; then
237         echo 1>&2 "$PRG: $boot: Not a regular file or block device"
238         local CONFERR=1
239     elif [ ! -w "$boot" -o ! -r "$boot" ] ; then
240         [ -z "$mntpoint" ] && echo 1>&2 "$PRG: $boot: Permission denied"
241         [ -z "$mntpoint" ] && CONFERR=1
242     fi
243
244     if [ ! -e "$install" ] ; then
245         echo 1>&2 "$PRG: $install: No such file or directory"
246         local CONFERR=1
247     elif [ ! -f "$install" ] ; then
248         echo 1>&2 "$PRG: $bootconf: Not a regular file"
249         local CONFERR=1
250     elif [ ! -r "$install" ] ; then
251         echo 1>&2 "$PRG: $install: Permission denied"
252         local CONFERR=1
253     fi
254
255     if [ "$bootconf" = auto ] ; then
256         true
257     elif [ ! -e "$bootconf" ] ; then
258         echo 1>&2 "$PRG: $bootconf: No such file or directory"
259         local CONFERR=1
260     elif [ ! -f "$bootconf" ] ; then
261         echo 1>&2 "$PRG: $bootconf: Not a regular file"
262         local CONFERR=1
263     elif [ ! -r "$bootconf" ] ; then
264         echo 1>&2 "$PRG: $bootconf: Permission denied"
265         local CONFERR=1
266     fi
267
268     if [ -n "$magicboot" ] ; then
269         if [ ! -e "$magicboot" ] ; then
270             echo 1>&2 "$PRG: $magicboot: No such file or directory"
271             local CONFERR=1
272         elif [ ! -f "$magicboot" ] ; then
273             echo 1>&2 "$PRG: $magicboot: Not a regular file"
274             local CONFERR=1
275         elif [ ! -r "$magicboot" ] ; then
276             echo 1>&2 "$PRG: $magicboot: Permission denied"
277             local CONFERR=1
278         fi
279     fi
280
281     case "$fstype" in
282         hfs|msdos|raw)
283          ;;
284         *)
285          if [ "$ARGFS" = 1 ] ; then
286             echo 1>&2 "$PRG: --filesystem must be either \`hfs', \`msdos', or \`raw'"
287          else
288             echo 1>&2 "$PRG:$ERR \`fstype' must be either \`hfs', \`msdos', or \`raw'"
289          fi
290          local CONFERR=1
291          ;;
292     esac
293
294     ## if we are not using HFS filesystems we don't care about HFS
295     ## specific options.
296     if [ "$fstype" = hfs ] ; then
297         if [ `echo ${#hfstype}` != 4 ] ; then
298             if [ "$ARGTP" = 1 ] ; then
299                 echo 1>&2 "$PRG: --type must be 4 characters"
300             else
301                 echo 1>&2 "$PRG:$ERR \`hfstype' must be 4 characters"
302             fi
303             local CONFERR=1
304         fi
305
306         if [ `echo ${#hfscreator}` != 4 ] ; then
307             if [ "$ARGCT" = 1 ] ; then
308                 echo 1>&2 "$PRG: --creator must be 4 characters"
309             else
310                 echo 1>&2 "$PRG:$ERR \`hfscreator' must be 4 characters"
311             fi
312             local CONFERR=1
313         fi
314     fi
315
316     ## some options are not compatible with fstype=raw
317     if [ "$fstype" = raw ] ; then
318         if [ -n "$mntpoint" ] ; then
319             echo 1>&2 "$PRG:$ERR \`mntpoint' is not compatible with fstype=raw"
320             local CONFERR=1
321         fi
322         if [ "$usemount" = yes ] ; then
323             echo 1>&2 "$PRG:$ERR \`usemount' is not compatible with fstype=raw"
324             local CONFERR=1
325         fi
326         if [ -n "$magicboot" ] ; then
327             echo 1>&2 "$PRG:$ERR \`magicboot' scripts cannot be used with fstype=raw"
328             local CONFERR=1
329         fi
330     fi
331
332     if [ -n "$mntpoint" ] ; then
333         ## standard checks
334         if [ ! -e "$mntpoint" ] ; then
335             echo 1>&2 "$PRG: $mntpoint: No such file or directory"
336             local CONFERR=1
337         elif [ ! -d "$mntpoint" ] ; then
338             echo 1>&2 "$PRG: $mntpoint: Not a directory"
339             local CONFERR=1
340         elif [ ! -w "$mntpoint" -o ! -r "$mntpoint" ] ; then
341             echo 1>&2 "$PRG: $mntpoint: Permission denied"
342             local CONFERR=1
343         elif [ ! -O "$mntpoint" -a `id -u` != 0 ] ; then
344             echo 1>&2 "$PRG: $mntpoint: Permission denied (not owner)"
345             local CONFERR=1
346         fi
347
348         ## make sure no embedded spaces exist
349         echo "$mntpoint" | grep -q [[:space:]]
350         if [ $? = 0 ] ; then
351             echo 1>&2 "$PRG:$ERR \`mntpoint=$mntpoint' contains embedded spaces, don't use lame filenames"
352             local CONFERR=1
353         fi
354
355         ## make sure $mntpoint is on $boot, this matters to nvram updating.
356         if [ "$(v=`df "$mntpoint" 2> /dev/null | grep ^/dev/` ; echo ${v%%[ ]*})" != "$boot" -a -d "$mntpoint" ] ; then
357             echo 1>&2 "$PRG: $mntpoint is not located on $boot"
358             local CONFERR=1
359             ## more then one subdirectory deep is not supported. no sed available on boot floppies ( / -> \ )
360         elif [ "$mntpoint" != "$(v=`df "$mntpoint" 2> /dev/null | grep ^/dev/` ; echo ${v##*[ ]})" ] ; then
361             echo "$(v=`df "$mntpoint" 2>/dev/null | grep ^/dev/`; m=${v##*[ ]}; echo "${mntpoint##*$m/}")" | grep -q /
362             if [ $? = 0 ] ; then
363                 echo 1>&2 "$PRG:$ERR $mntpoint is more then one subdirectory deep from root of $boot"
364                 local CONFERR=1
365             else
366                 OFDIR="$(v=`df "$mntpoint" 2>/dev/null | grep ^/dev/`; m=${v##*[ ]}; echo "${mntpoint##*$m/}")"
367             fi
368         fi
369
370         if [ "$usemount" = no ] ; then
371             echo 1>&2 "$PRG:$ERR \`mntpoint=' requires \`usemount' be set"
372             local CONFERR=1
373         fi
374     fi
375
376     if [ -n "$magicboot" ] ; then
377         ## check for bsd loader
378         if [ -n "$bsd" ] ; then
379             if [ -f /usr/lib/yaboot/ofwboot -a -r /usr/lib/yaboot/ofwboot ] ; then
380                 BSDLOADER="/usr/lib/yaboot/ofwboot"
381             elif [ -f /usr/local/lib/yaboot/ofwboot -a -r /usr/local/lib/yaboot/ofwboot ] ; then
382                 BSDLOADER="/usr/local/lib/yaboot/ofwboot"
383             else
384                 echo 1>&2 "$PRG: /usr/local/lib/yaboot/ofwboot: No such file or directory"
385                 echo 1>&2 "$PRG: With the bsd= option set you must have the bsd boot loader ofwboot"
386                 CONFERR=1
387             fi
388         fi
389
390         ## convert defaultos variable
391         case "$defaultos" in
392             linux|Linux|GNU|Gnu|gnu)
393                 defaultos=bootyaboot
394                 ;;
395             bootyaboot)
396                 ;;
397             bsd)
398                 defaultos=bootbsd
399                 if [ -z "$bsd" ] ; then
400                     echo 1>&2 "$PRG:$ERR no entry for \`bsd' found, but defaultos is set to \`bsd'"
401                     local CONFERR=1
402                 fi
403                 ;;
404             macos)
405                 defaultos=bootmacos
406                 if [ -z "$macos" ] ; then
407                     echo 1>&2 "$PRG:$ERR no entry for \`macos' found, but defaultos is set to \`macos'"
408                     local CONFERR=1
409                 fi
410                 ;;
411             macosx)
412                 defaultos=bootmacosx
413                 if [ -z "$macosx" ] ; then
414                     echo 1>&2 "$PRG:$ERR no entry for \`macosx' found, but defaultos is set to \`macosx'"
415                     local CONFERR=1
416                 fi
417                 ;;
418             darwin)
419                 defaultos=bootdarwin
420                 if [ -z "$darwin" ] ; then
421                     echo 1>&2 "$PRG:$ERR no entry for \`darwin' found, but defaultos is set to \`darwin'"
422                     local CONFERR=1
423                 fi
424                 ;;
425             *)
426                 echo 1>&2 "$PRG:$ERR \`defaultos' must be either \`linux', \`bsd', \`macos' or \`macosx'"
427                 local CONFERR=1
428                 ;;
429         esac
430     fi
431
432     ## nvsetenv requires /proc
433     if [ ! -f /proc/uptime -a "$nonvram" = 0 ] ; then
434         echo 1>&2 "$PRG: /proc filesystem is not mounted, nvram will not be updated"
435         nonvram=1
436     fi
437
438     if [ "$nonvram" = 0 ] ; then
439         ## see if nvsetenv exists and is executable
440         if (command -v nvsetenv > /dev/null 2>&1) ; then
441             [ -x `command -v nvsetenv` ] || MISSING=1 ; else MISSING=1
442         fi
443
444         if [ "$nonvram" = 0 ] ; then
445             ## if nvsetenv exists see if its the old broken version
446             if [ "$MISSING" != 1 ] ; then
447                 nvsetenv --version > /dev/null 2>&1 || OLD=1
448             else
449                 nonvram=1
450                 echo 1>&2 "$PRG: Warning: \`nvsetenv' could not be found, nvram will not be updated"
451             fi
452
453             if [ "$OLD" = 1 ] ; then
454                 ## i check this myself to avoid misleading error
455                 ## messages. nvsetenv should REALLY support --version.
456                 if [ ! -e /dev/nvram ] ; then
457                     echo 1>&2 "$PRG: /dev/nvram: No such file or directory"
458                     echo 1>&2 "$PRG: Warning: nvram will not be updated"
459                     nonvram=1
460                 elif [ ! -c /dev/nvram ] ; then
461                     echo 1>&2 "$PRG: /dev/nvram: Not a character device"
462                     echo 1>&2 "$PRG: Warning: nvram will not be updated"
463                     nonvram=1
464                 elif [ ! -w /dev/nvram -o ! -r /dev/nvram ] ; then
465                     echo 1>&2 "$PRG: /dev/nvram: Permission denied"
466                     echo 1>&2 "$PRG: Warning: nvram will not be updated"
467                     nonvram=1
468                 elif ! (dd if=/dev/nvram of=/dev/null bs=1 count=10 > /dev/null 2>&1) ; then
469                     echo 1>&2 "$PRG: /dev/nvram: No such device"
470                     echo 1>&2 "$PRG: Warning: nvram will not be updated"
471                     nonvram=1
472                 else
473                     nonvram=1
474                     echo 1>&2 "$PRG: Warning: Incompatible version of \`nvsetenv', nvram will not be updated"
475                 fi
476             fi
477         fi
478
479         if [ -f "$boot" ] ; then
480             echo 1>&2 "$PRG: $boot is a regular file, disabling nvram updating"
481             nonvram=1
482         fi
483     fi
484
485     ## check for newworld mac. use cat hack due to /proc wierdness.
486     ## do not bail if we are on an OldWorld only warn (very loudly).
487     if [ "$(v=`cat /proc/cpuinfo 2>/dev/null | grep pmac-generation` ; echo ${v##*:})" = NewWorld ] ; then
488         true
489     elif [ "$(v=`cat /proc/cpuinfo 2>/dev/null | grep pmac-generation` ; echo ${v##*:})" = OldWorld ] ; then
490         echo 1>&2
491         echo 1>&2 '@@@@@@@@@@@@@@ WARNING!! WARNING!! WARNING!! @@@@@@@@@@@@@@'
492         echo 1>&2 "$PRG: Warning: This is an OldWorld PowerMac, $boot will **NOT** be bootable on this machine"
493         echo 1>&2 "$PRG: Oldworld PowerMacs need to use the quik bootloader, not yaboot"
494         echo 1>&2 '@@@@@@@@@@@@@@ WARNING!! WARNING!! WARNING!! @@@@@@@@@@@@@@'
495         echo 1>&2
496         [ "$nonvram" = 0 ] && echo 1>&2 "$PRG: OldWorld PowerMac, nvram will not be updated"
497         nonvram=1
498     elif (cat /proc/cpuinfo 2>/dev/null | grep ^motherboard | grep -q AAPL) ; then
499         echo 1>&2
500         echo 1>&2 '@@@@@@@@@@@@@@ WARNING!! WARNING!! WARNING!! @@@@@@@@@@@@@@'
501         echo 1>&2 "$PRG: Warning: This is an OldWorld PowerMac, $boot will **NOT** be bootable on this machine"
502         echo 1>&2 "$PRG: Oldworld PowerMacs need to use the quik bootloader, not yaboot"
503         echo 1>&2 '@@@@@@@@@@@@@@ WARNING!! WARNING!! WARNING!! @@@@@@@@@@@@@@'
504         echo 1>&2
505         [ "$nonvram" = 0 ] && echo 1>&2 "$PRG: OldWorld PowerMac, nvram will not be updated"
506         nonvram=1
507     elif (cat /proc/cpuinfo 2> /dev/null | grep ^machine | grep -q 'CHRP IBM') ; then
508         ## IBM hardware does not need nvram update AFAICT
509         nonvram=1
510         ADDNOTE=yes
511     else
512         #echo 1>&2 "$PRG: Warning: Unknown archetecture, $boot may not be bootable on this machine"
513         [ "$nonvram" = 0 ] && echo 1>&2 "$PRG: Warning: Unknown architecture, nvram will not be updated"
514         nonvram=1
515     fi
516
517     ## convert human readable color values from config to proper color
518     ## codes
519     case "$fgcolor" in
520         black) fgc=0 ;; blue) fgc=1 ;; green) fgc=2 ;; cyan) fgc=3 ;;
521         red) fgc=4 ;; purple) fgc=5 ;; brown) fgc=6 ;; light-gray) fgc=7 ;;
522         dark-gray) fgc=8 ;; light-blue) fgc=9 ;; light-green) fgc=a ;;
523         light-cyan) fgc=b ;; light-red) fgc=c ;; light-purple) fgc=d ;;
524         yellow) fgc=e ;; white) fgc=f ;;
525         *)
526         echo 1>&2 "$PRG:$ERR Invalid fgcolor: \`$fgcolor'"
527         local CONFERR=1
528         ;;
529     esac
530     case "$bgcolor" in
531         black) bgc=0 ;; blue) bgc=1 ;; green) bgc=2 ;; cyan) bgc=3 ;;
532         red) bgc=4 ;; purple) bgc=5 ;; brown) bgc=6 ;; light-gray) bgc=7 ;;
533         dark-gray) bgc=8 ;; light-blue) bgc=9 ;; light-green) bgc=a ;;
534         light-cyan) bgc=b ;; light-red) bgc=c ;; light-purple) bgc=d ;;
535         yellow) bgc=e ;; white) bgc=f ;;
536         *)
537         echo 1>&2 "$PRG:$ERR Invalid bgcolor: \`$bgcolor'"
538         local CONFERR=1
539         ;;
540     esac
541
542     ## if delay is not set use yaboot's timeout
543     if [ -z "$delay" ] ; then
544         delay="$(($timeout / 10))"
545     fi
546
547     if [ "$CONFERR" = 1 ] ; then
548         return 1
549     else
550         return 0
551     fi
552 }
553
554
555 ## if readlink is missing use a kludge
556 if (command -v readlink > /dev/null 2>&1) ; then
557     true
558 else
559     readlink()
560     {
561         local SYMTARGET="$(v=`ls -l "$2" 2>/dev/null` ; echo ${v##*> })"
562         if [ -n "$SYMTARGET" ] ; then
563             echo "$SYMTARGET"
564             return 0
565         else
566             return 1
567         fi
568     }
569 fi
570
571 ## /etc/yaboot.conf with password should not be world readable.
572 permcheck()
573 {
574 if [ -L "$bootconf" ] ; then
575     local realfile="$(readlink -f "$bootconf")" || return 0
576 else
577     local realfile="$bootconf"
578 fi
579
580 ## don't bother if we could not read the symlink
581 [ -z "$realfile" ] && return 0
582 [ ! -f "$realfile" ] && return 0
583
584 ## get permissions, and don't bother checking if we can't
585 local PERM=`v=$(ls -l "$realfile" 2>/dev/null) ; echo ${v%% *}`
586 [ -z "$PERM" ] && return 0
587 [ `echo ${#PERM}` != 10 ] && return 0
588
589 case "$PERM" in
590     -rw-------|-r--------)
591       if [ ! -O "$realfile" -a `id -u` = 0 ] ; then
592          echo 1>&2 "$PRG: Warning: $bootconf is not owned by root"
593       fi
594       ;;
595     -rw-r-----|-r--r-----)
596       if [ ! -O "$realfile" -a `id -u` = 0 ] ; then
597          echo 1>&2 "$PRG: Warning: $bootconf is not owned by root"
598       fi
599       if [ ! -G "$realfile" -a `id -g` = 0 ] ; then
600          echo 1>&2 "$PRG: Warning: $bootconf is not owned by group root"
601       fi
602       ;;
603     -r--r--r--|-rw-r--r--|-rw-rw-r--|-rw-rw-rw-|-rw-rw----)
604       echo 1>&2 "$PRG: Warning: Insecure permissions on $bootconf: $PERM should be -rw-------"
605       ;;
606     *)
607       echo 1>&2 "$PRG: Warning: Incorrect permissions on $bootconf: $PERM should be -rw-------"
608       ;;
609 esac
610 }
611
612 convertpath()
613 {
614     ## figure out bootstrap device OF pathname if user did not supply it.
615     if [ -z "$ofboot" ] ; then
616         [ "$VERBOSE" = 1 ] && echo "$PRG: Finding OpenFirmware device path to \`$boot'..."
617         ofboot="$($OFPATH $boot)"
618         if [ $? != 0 ] ; then
619             echo 1>&2 "$PRG: Unable to find OpenFirmware path for boot=$boot"
620             echo 1>&2 "$PRG: Please add ofboot=<path> where <path> is the OpenFirmware path to $boot to $CONF"
621             local CONVERR=1
622         fi
623         [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: ofboot set to \`$ofboot'"
624     fi
625
626     ## figure out OF device path for macos/macosx if user supplied a unix device node.
627     if [ -n "$bsd" ] ; then
628         case "$bsd" in
629             /dev/*)
630                 [ "$VERBOSE" = 1 ] && echo "$PRG: Finding OpenFirmware device path to \`$bsd'..."
631                 local sbsd="$bsd"
632                 bsd="$($OFPATH $bsd)"
633                 if [ $? != 0 ] ; then
634                     echo 1>&2 "$PRG: Unable to determine OpenFirmware path for bsd=$sbsd"
635                     echo 1>&2 "$PRG: Try specifying the real OpenFirmware path for bsd=$sbsd in $CONF"
636                     local CONVERR=1
637                 fi
638                 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: bsd set to \`$bsd' from \`$sbsd'"
639                 ;;
640             *)
641                 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: bsd left alone: \`$bsd'"
642                 ;;
643         esac
644     fi
645
646     if [ -n "$macos" ] ; then
647         case "$macos" in
648             /dev/*)
649                 [ "$VERBOSE" = 1 ] && echo "$PRG: Finding OpenFirmware device path to \`$macos'..."
650                 local smacos="$macos"
651                 macos="$($OFPATH $macos)"
652                 if [ $? != 0 ] ; then
653                     echo 1>&2 "$PRG: Unable to determine OpenFirmware path for macos=$smacos"
654                     echo 1>&2 "$PRG: Try specifying the real OpenFirmware path for macos=$smacos in $CONF"
655                     local CONVERR=1
656                 fi
657                 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: macos set to \`$macos' from \`$smacos'"
658                 ;;
659             *)
660                 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: macos left alone: \`$macos'"
661                 ;;
662         esac
663     fi
664
665     if [ -n "$macosx" ] ; then
666         case "$macosx" in
667             /dev/*)
668                 [ "$VERBOSE" = 1 ] && echo "$PRG: Finding OpenFirmware device path to \`$macosx'..."
669                 local smacosx="$macosx"
670                 macosx="$($OFPATH $macosx)"
671                 if [ $? != 0 ] ; then
672                     echo 1>&2 "$PRG: Unable to determine OpenFirmware path for macosx=$smacosx"
673                     echo 1>&2 "$PRG: Try specifying the real OpenFirmware path for macosx=$smacosx in $CONF"
674                     local CONVERR=1
675                 fi
676                 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: macosx set to \`$macosx' from \`$smacosx'"
677                 ;;
678             *)
679                 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: macosx left alone: \`$macosx'"
680                 ;;
681         esac
682     fi
683
684     if [ -n "$darwin" ] ; then
685         case "$darwin" in
686             /dev/*)
687                 [ "$VERBOSE" = 1 ] && echo "$PRG: Finding OpenFirmware device path to \`$darwin'..."
688                 local sdarwin="$darwin"
689                 darwin="$($OFPATH $darwin)"
690                 if [ $? != 0 ] ; then
691                     echo 1>&2 "$PRG: Unable to determine OpenFirmware path for darwin=$sdarwin"
692                     echo 1>&2 "$PRG: Try specifying the real OpenFirmware path for darwin=$sdarwin in $CONF"
693                     local CONVERR=1
694                 fi
695                 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: darwin set to \`$darwin' from \`$sdarwin'"
696                 ;;
697             *)
698                 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: darwin left alone: \`$darwin'"
699                 ;;
700         esac
701     fi
702
703     if [ "$CONVERR" = 1 ] ; then
704         return 1
705     else
706         return 0
707     fi
708 }
709
710 ## make sure the hfsutils we need are installed and executable.
711 checkhfsutils()
712 {
713     if (command -v hmount > /dev/null 2>&1) ; then
714         [ -x `command -v hmount` ] || FAIL=1 ; else FAIL=1 ; fi
715     if (command -v humount > /dev/null 2>&1) ; then
716         [ -x `command -v humount` ] || FAIL=1 ; else FAIL=1 ; fi
717     if (command -v hcopy > /dev/null 2>&1) ; then
718         [ -x `command -v hcopy` ] || FAIL=1 ; else FAIL=1 ; fi
719     if (command -v hattrib > /dev/null 2>&1) ; then
720         [ -x `command -v hattrib` ] || FAIL=1 ; else FAIL=1 ; fi
721     if (command -v hformat > /dev/null 2>&1) ; then
722         [ -x `command -v hformat` ] || FAIL=1 ; else FAIL=1 ; fi
723
724     if [ "$FAIL" = 1 ] ; then
725         return 1
726     else
727         return 0
728     fi
729 }
730
731 ## This is gross, IBM CHRP OF needs a .note added to the yaboot
732 ## binary, nobody knows whether this note will affect PowerMac OF or
733 ## not (or could in the future).
734 hack_yaboot()
735 {
736     local YBDIR="${install%/*}"
737     if [ -x "$YBDIR/addnote" ] ; then
738         TMPYABOOT=`mktemp -q "$TMP/yaboot.XXXXXX"`
739         if [ $? != 0 ] ; then
740             echo 1>&2 "$PRG: Could not create temporary file, aborting."
741             return 1
742         else
743             if (cat "$install" > "$TMPYABOOT" 2> /dev/null) ; then
744                 install="$TMPYABOOT"
745             else
746                 echo 1>&2 "$PRG: Could not create temporary file, aborting."
747                 return 1
748             fi
749         fi
750         [ "$DEBUG" = 1 ] && echo "$PRG: Embedding CHRP note section in temp yaboot..."
751         "$YBDIR/addnote" "$install" > /dev/null 2>&1
752         if [ $? != 0 ] ; then
753             echo 1>&2 "$PRG: Could not install note section required by your architecture"
754             return 1
755         fi
756     else
757         echo 1>&2 "$PRG: Your architecture requires $YBDIR/addnote which cannot be found"
758         return 1
759     fi
760     return 0
761 }
762
763 ## install using userspace utilities rather then kernel filesytem
764 ## support.  hfsutils only, mtools not supported.
765 util_install()
766 {
767     ## catch signals, and humount, cleanup done by trap 0.
768     trap "humount $boot ; exit 129" 1
769     trap "echo 1>&2 $SIGINT ; humount $boot ; exit 130" 2
770     trap "humount $boot ; exit 131" 3
771     trap "humount $boot ; exit 143" 15
772
773     ## filenames on bootstrap partition. ofboot hard codes yaboot.
774     local BTFILE=yaboot
775     local CFFILE=yaboot.conf
776
777     ## if there is a magicboot script to install we will give it the
778     ## hfstype (should be "tbxi") and give yaboot type "boot".
779     if [ -n "$magicboot" ] ; then
780         local BTTYPE=boot
781     else
782         local BTTYPE="$hfstype"
783     fi
784
785     if [ -n "$magicboot" ] ; then
786         local WRAP="${magicboot##*/}"
787     fi
788
789     ## set verbose messages here so they don't show temporary file paths
790     local INSTALLFIRST="$PRG: Installing first stage bootstrap $magicboot onto $boot..."
791     local INSTALLPRIMARY="$PRG: Installing primary bootstrap $install onto $boot..."
792
793     ## repoint magicboot as the real first stage loader if using the
794     ## modern automatic generating ofboot.b.
795     if [ -n "$FIRST" ] ; then
796         magicboot="$FIRST"
797         [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: set magicboot to $FIRST"
798     fi
799
800     ## gross hack, add note section for IBM CHRP
801     if [ "$ADDNOTE" = yes ] ; then
802         hack_yaboot || return 1
803     fi
804
805     if [ "$fstype" = hfs ] ; then
806         if [ "$protect" = yes ] ; then
807             local LOCK="+l"
808         fi
809
810         if [ "$hide" = yes ] ; then
811             local INVISIBLE="+i"
812         fi
813
814         ## make sure the device is not mounted as a filesystem before
815         ## we start mucking with it directly.
816         mount | grep "^$boot\>" > /dev/null
817         if [ $? = 0 ] ; then
818             echo 1>&2 "$PRG: $boot appears to be mounted! aborting."
819             return 1
820         fi
821
822         ## hmount is really more of a way to make sure we have a valid HFS
823         ## filesystem before proceding, and hcopy requires it...
824         hmount "$boot" > /dev/null
825         if [ $? != 0 ] ; then
826             echo 1>&2 "$PRG: $boot appears to have never had a bootstrap installed, please run mkofboot"
827             return 1
828         fi
829
830         ## must be explicit with target filename to avoid hfsutils
831         ## braindamage ("_" -> " " filename mangling) also avoid
832         ## ambiguity in the bootstrap partition.
833         if [ -n "$magicboot" ] ; then
834             [ "$VERBOSE" = 1 ] && echo "$INSTALLFIRST"
835             hcopy -r "$magicboot" :ofboot.b
836             if [ $? != 0 ] ; then
837                echo 1>&2 "$PRG: An error occured while writing to $boot"
838                return 1
839             fi
840         fi
841
842         [ "$VERBOSE" = 1 ] && echo "$INSTALLPRIMARY"
843         hcopy -r "$install" :"$BTFILE"
844         if [ $? != 0 ] ; then
845             echo 1>&2 "$PRG: An error occured while writing to $boot"
846             return 1
847         fi
848
849         [ "$VERBOSE" = 1 ] && echo "$PRG: Installing $bootconf onto $boot..."
850         hcopy -r "$bootconf" :"$CFFILE"
851         if [ $? != 0 ] ; then
852             echo 1>&2 "$PRG: An error occured while writing to $boot"
853             return 1
854         fi
855
856         if [ -n "$BSDLOADER" ] ; then
857             [ "$VERBOSE" = 1 ] && echo "$PRG: Installing $BSDLOADER onto $boot..."
858             hcopy -r "$BSDLOADER" :ofwboot
859             if [ $? != 0 ] ; then
860                 echo 1>&2 "$PRG: An error occured while writing to $boot"
861                 return 1
862             fi
863         fi
864
865         ## set all file's attributes, if a magicboot script exists it
866         ## gets the configured hfstype instead of yaboot (should be
867         ## "tbxi") so it gets booted by OF.
868         if [ -n "$magicboot" ] ; then
869             [ "$VERBOSE" = 1 ] && echo "$PRG: Setting attributes on $WRAP..."
870             hattrib -t "$hfstype" -c "$hfscreator" $INVISIBLE $LOCK :ofboot.b
871             if [ $? != 0 ] ; then
872                 echo 1>&2 "$PRG: Warning: error setting attributes on $WRAP"
873                 echo 1>&2 "$PRG: This is probably bad but we'll ignore it."
874             fi
875         fi
876
877         [ "$VERBOSE" = 1 ] && echo "$PRG: Setting attributes on $BTFILE..."
878         hattrib -t "$BTTYPE" -c "$hfscreator" $INVISIBLE $LOCK :"$BTFILE"
879         if [ $? != 0 ] ; then
880             echo 1>&2 "$PRG: Warning: error setting attributes on $BTFILE"
881             echo 1>&2 "$PRG: This is probably bad but we'll ignore it"
882         fi
883
884         [ "$VERBOSE" = 1 ] && echo "$PRG: Setting attributes on $CFFILE..."
885         hattrib -t "conf" -c "$hfscreator" $INVISIBLE $LOCK :"$CFFILE"
886         if [ $? != 0 ] ; then
887             echo 1>&2 "$PRG: Warning: error setting attributes on $CFFILE"
888             echo 1>&2 "$PRG: This is probably unimportant so we'll ignore it"
889         fi
890
891         if [ -n "$BSDLOADER" ] ; then
892             [ "$VERBOSE" = 1 ] && echo "$PRG: Setting attributes on ofwboot..."
893             hattrib -t "bsdb" -c "$hfscreator" $INVISIBLE $LOCK :ofwboot
894             if [ $? != 0 ] ; then
895                 echo 1>&2 "$PRG: Warning: error setting attributes on ofwboot"
896                 echo 1>&2 "$PRG: This is probably unimportant so we'll ignore it"
897             fi
898         fi
899
900         ## bless the root directory so OF will find the boot file
901         if [ "$bless" = yes ] ; then
902             [ "$VERBOSE" = 1 ] && echo "$PRG: Blessing $boot with Holy Penguin Pee..."
903             hattrib -b :
904             if [ $? != 0 ] ; then
905                 echo 1>&2 "$PRG: Warning: error blessing $boot"
906                 echo 1>&2 "$PRG: This is probably bad but we'll ignore it"
907             fi
908         fi
909
910         ## clean up the ~/.hcwd file hmount creates
911         humount "$boot" > /dev/null
912         sync ; sync
913
914         ## use explicit filename if we don't bless.
915         if [ "$bless" = yes ] ; then
916             local OFFILE='\\:tbxi'
917         else
918             if [ -n "$magicboot" ] ; then
919                 local OFFILE=ofboot.b
920             else
921                 local OFFILE="$BTFILE"
922             fi
923         fi
924
925         ## update the boot-device variable in OF nvram.
926         if [ "$nonvram" = 0 ] ; then
927             [ "$VERBOSE" = 1 ] && echo "$PRG: Updating OpenFirmware boot-device variable in nvram..."
928             [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: boot-device=${ofboot},${OFFILE}"
929             nvsetenv boot-device "${ofboot},${OFFILE}"
930             if [ $? != 0 ] ; then
931                 echo 1>&2 "$PRG: An error occured while updating nvram, we'll ignore it"
932             fi
933         fi
934
935     else
936         echo 1>&2 "$PRG: mtools support is not implemented"
937         echo 1>&2 "$PRG: Use --mount or add \`usemount' to $CONF"
938         return 1
939     fi
940
941     return 0
942 }
943
944 ## used by mnt_install so mntpoint= can be supported in a cleaner way.
945 mnt()
946 {
947     ## we can even create bootstrap filesystem images directly if you
948     ## ever wanted too.
949     if [ -f "$boot" ] ; then
950         local loop=",loop"
951     fi
952
953     if [ -e "$TMP/bootstrap.$$" ] ; then
954         echo 1>&2 "$PRG: $TMP/bootstrap.$$ exists, aborting."
955         return 1
956     fi
957
958     mkdir -m 700 "$TMP/bootstrap.$$"
959     if [ $? != 0 ] ; then
960         echo 1>&2 "$PRG: Could not create mountpoint directory, aborting."
961         return 1
962     fi
963
964     mount | grep "^$boot\>" > /dev/null
965     if [ $? = 0 ] ; then
966         echo 1>&2 "$PRG: $boot appears to be mounted! aborting."
967         return 1
968     fi
969
970     [ "$VERBOSE" = 1 ] && echo "$PRG: Mounting $boot..."
971     mount -t "$fstype" -o rw,umask=077$loop "$boot" "$TMP/bootstrap.$$"
972     if [ $? != 0 ] ; then
973         echo 1>&2 "$PRG: An error occured mounting $boot"
974         return 1
975     fi
976
977     ## catch signals, set here to avoid umounting something we did not
978     ## mount. cleanup done by trap 0.
979     trap "umount $boot ; exit 129" 1
980     trap "echo 1>&2 $SIGINT ; umount $boot ; exit 130" 2
981     trap "umount $boot ; exit 131" 3
982     trap "umount $boot ; exit 143" 15
983
984     TARGET="$TMP/bootstrap.$$"
985     return 0
986 }
987
988 ## umnt funtion which checks whether we mounted anything or not, for
989 ## mntpoint= this makes the code below cleaner IMO.
990 umnt()
991 {
992     if [ -z "$mntpoint" ] ; then
993         [ "$1" = failure ] && echo 1>&2 "$PRG: Attempting to umount $boot..."
994         umount "$2"
995         if [ $? != 0 ] ; then
996             echo 1>&2 "$PRG: umount of $boot failed!"
997             return 1
998         else
999             [ "$1" = failure ] && echo 1>&2 "$PRG: umount successfull"
1000             return 0
1001         fi
1002     else
1003         return 0
1004     fi
1005 }
1006
1007 ## Use kernel filesytem drivers to mount the bootstrap partition like
1008 ## any other filesystem and copy the files there with standard un*x
1009 ## utilities.
1010 mnt_install()
1011 {
1012     local BTFILE=yaboot
1013
1014     ## msdosfs is broken, yaboot may not support this filename.
1015     if [ "$fstype" = msdos ] ; then
1016         local CFFILE=yaboot.cnf
1017     else
1018         local CFFILE=yaboot.conf
1019     fi
1020
1021     if [ -n "$magicboot" ] ; then
1022         local WRAP="${magicboot##*/}"
1023     fi
1024
1025     ## set verbose messages here so they don't show temporary file paths
1026     local INSTALLFIRST="$PRG: Installing first stage bootstrap $magicboot onto $boot..."
1027     local INSTALLPRIMARY="$PRG: Installing primary bootstrap $install onto $boot..."
1028
1029     ## repoint magicboot as the real first stage loader if using the
1030     ## modern automatic generating ofboot.b.
1031     if [ -n "$FIRST" ] ; then
1032         magicboot="$FIRST"
1033         [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: set magicboot to $FIRST"
1034     fi
1035
1036     ## gross hack, add note section for IBM CHRP
1037     if [ "$ADDNOTE" = yes ] ; then
1038         hack_yaboot || return 1
1039     fi
1040
1041     ## call mnt() function to take care of mounting filesystem if needed
1042     if [ -z "$mntpoint" ] ; then
1043         mnt || return 1
1044     else
1045         TARGET="$mntpoint"
1046     fi
1047
1048     ## this is probably insecure on modern filesystems, but i think
1049     ## safe on crippled hfs/dosfs. user should ensure mntpoint= is safe.
1050     if [ -n "$magicboot" ] ; then
1051         [ "$VERBOSE" = 1 ] && echo "$INSTALLFIRST"
1052         cp -f "$magicboot" "$TARGET/ofboot.b"
1053         if [ $? != 0 ] ; then
1054             echo 1>&2 "$PRG: An error occured while writing to $boot"
1055             umnt failure "$TARGET"
1056             return 1
1057         fi
1058     fi
1059
1060     [ "$VERBOSE" = 1 ] && echo "$INSTALLPRIMARY"
1061     cp -f "$install" "$TARGET/$BTFILE"
1062     if [ $? != 0 ] ; then
1063         echo 1>&2 "$PRG: An error occured while writing to $boot"
1064         umnt failure "$TARGET"
1065         return 1
1066     fi
1067
1068     [ "$VERBOSE" = 1 ] && echo "$PRG: Installing $bootconf onto $boot..."
1069     cp -f "$bootconf" "$TARGET/$CFFILE"
1070     if [ $? != 0 ] ; then
1071         echo 1>&2 "$PRG: An error occured while writing to $boot"
1072         umnt failure "$TARGET"
1073         return 1
1074     fi
1075
1076     if [ -n "$BSDLOADER" ] ; then
1077         [ "$VERBOSE" = 1 ] && echo "$PRG: Installing $BSDLOADER onto $boot..."
1078         cp -f "$BSDLOADER" "$TARGET/ofwboot"
1079         if [ $? != 0 ] ; then
1080             echo 1>&2 "$PRG: An error occured while writing to $boot"
1081             umnt failure "$TARGET"
1082             return 1
1083         fi
1084     fi
1085
1086     if [ "$protect" = yes ] ; then
1087         [ "$VERBOSE" = 1 ] && echo "$PRG: Setting read-only attributes..."
1088         chmod a-w "$TARGET/$BTFILE"
1089         chmod a-w "$TARGET/$CFFILE"
1090         if [ -n "$magicboot" ] ; then
1091             chmod a-w "$TARGET/ofboot.b"
1092         fi
1093         if [ -n "$BSDLOADER" ] ; then
1094             chmod a-w "$TARGET/ofwboot"
1095         fi
1096     fi
1097
1098     sync ; sync
1099     umnt success "$TARGET" || return 1
1100
1101     ## make variable with a \ to avoid shell fsckage.  ugly ugly ugly.
1102     local BS='\'
1103     if [ -n "$magicboot" ] ; then
1104         [ -n "$OFDIR" ] && OFDIR="${BS}${OFDIR}${BS}"
1105         local OFFILE="${OFDIR}ofboot.b"
1106     else
1107         [ -n "$OFDIR" ] && OFDIR="${BS}${OFDIR}${BS}"
1108         local OFFILE="${OFDIR}${BTFILE}"
1109     fi
1110
1111     ## update the boot-device variable in OF nvram.
1112     if [ "$nonvram" = 0 ] ; then
1113         [ "$VERBOSE" = 1 ] && echo "$PRG: Updating OpenFirmware boot-device variable in nvram..."
1114         [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: boot-device=${ofboot},${OFFILE}"
1115         nvsetenv boot-device "${ofboot},${OFFILE}"
1116         if [ $? != 0 ] ; then
1117             echo 1>&2 "$PRG: An error occured while updating nvram, we'll ignore it"
1118         fi
1119     else
1120         echo 1>&2 "$PRG: Warning: You must manually configure OpenFirmware to boot."
1121     fi
1122
1123     return 0
1124 }
1125
1126 ## raw installation, for IBM RS/6000 hardware, yaboot is dded to the
1127 ## bootstrap partition.
1128 raw_install()
1129 {
1130     ## make sure the device is not mounted as a filesystem before
1131     ## we start mucking with it directly.
1132     mount | grep "^$boot\>" > /dev/null
1133     if [ $? = 0 ] ; then
1134         echo 1>&2 "$PRG: $boot appears to be mounted! aborting."
1135         return 1
1136     fi
1137
1138     ## set verbosity message before munging the yaboot pathname
1139     local INSTALLPRIMARY="$PRG: Installing primary bootstrap $install onto $boot..."
1140
1141     ## gross hack, add note section for IBM CHRP
1142     if [ "$ADDNOTE" = yes ] ; then
1143         hack_yaboot || return 1
1144     fi
1145
1146     [ "$VERBOSE" = 1 ] && echo "$INSTALLPRIMARY"
1147     dd if=/dev/zero of="$boot" bs=512 count=1600 > /dev/null 2>&1
1148     dd if="$install" of="$boot" bs=512 > /dev/null 2>&1
1149     if [ $? != 0 ] ; then
1150         echo 1>&2 "$PRG: Installation failed."
1151         return 1
1152     fi
1153     sync ; sync
1154     [ "$VERBOSE" = 1 ] && echo "$PRG: Installation successful"
1155     return 0
1156 }
1157
1158 ## make sure the first stage ofboot generator is compatible.
1159 checkfirststage()
1160 {
1161     grep -q "^#%ybinscript-" "$magicboot" 2> /dev/null
1162     if [ "$?" = 0 ] ; then
1163         local magic=`grep "^#%ybinscript-" "$magicboot"`
1164         local ver="${magic##*-}"
1165         if [ "$ver" = "1.1" ] ; then
1166             FIRSTSTG=compat
1167             return 0
1168         else
1169             echo 1>&2 "$PRG: Incompatible version of first stage loader $magicboot.  aborting..."
1170             return 1
1171         fi
1172     else
1173         FIRSTSTG=old
1174         return 0
1175     fi
1176 }
1177
1178 ## build the first stage loader.
1179 mkfirststage()
1180 {
1181     ## must have 7 backslashes to == \\ printf + shell = bizarre... or,
1182     ## make special variable to contain a \ (need \\ to make \) to work
1183     ## around echo -e -n brokeness.
1184     local BS='\\'
1185     local OS=1
1186
1187     ## deal with mntpoint=
1188     [ -n "$OFDIR" ] && local OFDIR="${BS}${OFDIR}${BS}"
1189
1190     ## some misguided people insist on installing OSX on
1191     ## HorribleFileSystem+ instead of UFS, as a result MacOS deblesses
1192     ## OSX, making it unbootable. if apple localizes the filesystem hierarchy again screw it.
1193     [ "$brokenosx" = yes ] && local OSXBOOT="${BS}System${BS}Library${BS}CoreServices${BS}BootX"
1194     [ "$brokenosx" = no ] && local OSXBOOT="${BS}${BS}:tbxi"
1195
1196     ## assign variables for configured menu options.
1197     [ "$usemount" = no -a "$bless" = yes ] && local YB="yaboot GNU l $ofboot ,${BS}${BS}yaboot"
1198     [ "$usemount" = yes -o "$bless" = no ] && local YB="yaboot GNU l $ofboot ,${OFDIR}yaboot"
1199     [ -n "$bsd" ] && OS="$(($OS + 1))" && local BSD="ybsd BSD b $ofboot ,${BS}${BS}ofwboot/$bsd"
1200     [ -n "$macos" ] && OS="$(($OS + 1))" && local MAC="macos MacOS m $macos ,${BS}${BS}:tbxi"
1201     [ -n "$macosx" ] && OS="$(($OS + 1))" && local MX="macosx MacOSX x $macosx ,${OSXBOOT}"
1202     [ -n "$darwin" ] && OS="$(($OS + 1))" && local DW="darwin Darwin d $darwin ,${BS}${BS}:tbxi"
1203     [ "$cdrom" = yes ] && OS="$(($OS + 1))" && local CD="cd CDROM c cd: ,${BS}${BS}:tbxi"
1204     [ "$network" = yes ] && OS="$(($OS + 1))" && local NET="net Network n enet: 0"
1205     [ "$of" = yes ] && OS="$(($OS + 1))" && local OF="of OpenFirmware o quit now"
1206     [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: OS=$OS"
1207
1208     ## call ofboot,
1209     ## Usage: OS-count defaultos timeout fgc bgc osname oslabel oskey osdev osfile ...
1210     [ "$DEBUG" = 1 ] && $PRINTF 1>&2 "$PRG: DEBUG: /bin/sh $magicboot $OS $defaultos $delay $fbc $bgc $YB $BSD $MAC $MX $DW $CD $NET $OF\n"
1211     FIRST="$(/bin/sh "$magicboot" "$OS" "$defaultos" "$delay" $fgc $bgc ${YB} ${BSD} ${MAC} ${MX} ${DW} ${CD} ${NET} ${OF})" || return 1
1212
1213     return 0
1214 }
1215
1216 ## mkofboot function.
1217 mkoffs()
1218 {
1219     mount | grep "^$boot\>" > /dev/null
1220     if [ $? = 0 ] ; then
1221         echo 1>&2 "$PRG: $boot appears to be mounted! aborting."
1222         return 1
1223     fi
1224
1225     case "$fstype" in
1226         hfs)
1227             [ "$VERBOSE" = 1 ] && echo "$PRG: Creating HFS filesystem on $boot..."
1228             if (command -v dd > /dev/null 2>&1) ; then
1229                 dd if=/dev/zero of="$boot" bs=512 count=1600 > /dev/null 2>&1
1230             fi
1231             hformat -l bootstrap "$boot" > /dev/null
1232             if [ $? != 0 ] ; then
1233                 echo 1>&2 "$PRG: HFS filesystem creation failed!"
1234                 return 1
1235             fi
1236             humount "$boot" ## otherwise we might get confused.
1237             return 0
1238             ;;
1239         msdos)
1240             if (command -v mkdosfs > /dev/null 2>&1) ; then
1241                 [ -x `command -v mkdosfs` ] || FAIL=1 ; else FAIL=1 ; fi
1242                 if [ "$FAIL" = 1 ] ; then
1243                     echo 1>&2 "$PRG: mkdosfs is not installed or cannot be found"
1244                     return 1
1245                 fi
1246
1247             [ "$VERBOSE" = 1 ] && echo "$PRG: Creating DOS filesystem on $boot..."
1248             if (command -v dd > /dev/null 2>&1) ; then
1249                 dd if=/dev/zero of="$boot" bs=512 count=1600 > /dev/null 2>&1
1250             fi
1251             mkdosfs -n bootstrap "$boot" > /dev/null
1252             if [ $? != 0 ] ; then
1253                 echo 1>&2 "$PRG: DOS filesystem creation failed!"
1254                 return 1
1255             fi
1256             return 0
1257             ;;
1258     esac
1259 }
1260
1261 confirm()
1262 {
1263     if [ "$FORCE" = yes ] ; then
1264         return 0
1265     else
1266         echo 1>&2
1267         [ "$fstype" = raw ] && $PRINTF 1>&2 "$PRG: Overwrite contents of $boot with $install? [y/N] "
1268         [ "$fstype" != raw ] && $PRINTF 1>&2 "$PRG: Create $fstype filesystem on $boot? [y/N] "
1269         read ans
1270         case "$ans" in
1271             y|Y)
1272                 return 0
1273                 ;;
1274             *)
1275                 echo 1>&2 "$PRG: Abort."
1276                 return 2
1277                 ;;
1278         esac
1279     fi
1280 }
1281
1282 ## for fstype=raw check if an ELF binary has already been dded.
1283 luserck()
1284 {
1285     if [ "$(dd if="$boot" bs=1 skip=1 count=3 2>/dev/null)" = ELF ] ; then
1286         return 0
1287     else
1288         echo 1>&2 "$PRG: This partition has never had yaboot installed before, please run mkofboot"
1289         return 1
1290     fi
1291 }
1292
1293 mkconf()
1294 {
1295 ## defaults for this are defined at the beginning of the script with
1296 ## other variables.
1297
1298 echo \
1299 "## yaboot configuration file generated by ybin $VERSION
1300
1301 device=$device
1302 timeout=$timeout
1303
1304 image=$image
1305         label=$label
1306         partition=$partition
1307         root=$root
1308         read-only
1309 " > "$TMPCONF" || return 1
1310
1311 [ "$DEBUG" = 1 ] && $PRINTF 1>&2 "\nDEBUG: autoconf:\n----\n" && cat "$TMPCONF" 1>&2 && echo 1>&2 "----"
1312 return 0
1313 }
1314
1315 ## take out the trash.
1316 cleanup()
1317 {
1318     if [ -n "$TMPCONF" ] ; then rm -f "$TMPCONF" ; fi
1319     if [ -n "$FIRST" ] ; then rm -f "$FIRST" ; fi
1320     if [ -n "$TMPYABOOT" ] ; then rm -f "$TMPYABOOT" ; fi
1321     if [ -d "$TMP/bootstrap.$$" -a "$usemount" = yes ] ; then rmdir "$TMP/bootstrap.$$" ; fi
1322     return 0
1323 }
1324
1325 ##########
1326 ## Main ##
1327 ##########
1328
1329 ## absurdly bloated case statement to parse command line options.
1330 if [ $# != 0 ] ; then
1331     while true ; do
1332         case "$1" in
1333             -V|--version)
1334                 version
1335                 exit 0
1336                 ;;
1337             -h|--help)
1338                 usage
1339                 exit 0
1340                 ;;
1341             --debug)
1342                 DEBUG=1
1343                 ARGS="$ARGS $1"
1344                 shift
1345                 ;;
1346             -v|--verbose)
1347                 VERBOSE=1
1348                 ARGS="$ARGS $1"
1349                 shift
1350                 ;;
1351             -f|--force)
1352                 FORCE=yes
1353                 ARGS="$ARGS $1"
1354                 shift
1355                 ;;
1356             -b|--boot)
1357                 if [ -n "$2" ] ; then
1358                     if [ "$boot" = "unconfigured" ]; then
1359                         boot="$2"
1360                     else
1361                         boot="$boot $2"
1362                     fi
1363                     ARGBT=1
1364                     shift 2
1365                 else
1366                     echo 1>&2 "$PRG: option requires an argument $1"
1367                     echo 1>&2 "Try \`$PRG --help' for more information."
1368                     exit 1
1369                 fi
1370                 ;;
1371             -o|--ofboot)
1372                 if [ -n "$2" ] ; then
1373                     ofboot="$2"
1374                     ARGOB=1
1375                     ARGS="$ARGS $1 $2"
1376                     shift 2
1377                 else
1378                     echo 1>&2 "$PRG: option requires an argument $1"
1379                     echo 1>&2 "Try \`$PRG --help' for more information."
1380                     exit 1
1381                 fi
1382                 ;;
1383             --bootonce)
1384                 if [ -n "$2" ] ; then
1385                     bootonce="$2"
1386                     ARGS="$ARGS $1 $2"
1387                     shift 2
1388                 else
1389                     echo 1>&2 "$PRG: option requires an argument $1"
1390                     echo 1>&2 "Try \`$PRG --help' for more information."
1391                     exit 1
1392                 fi
1393                 ;;
1394             -i|--install)
1395                 if [ -n "$2" ] ; then
1396                     install="$2"
1397                     ARGBF=1
1398                     ARGS="$ARGS $1 $2"
1399                     shift 2
1400                 else
1401                     echo 1>&2 "$PRG: option requires an argument $1"
1402                     echo 1>&2 "Try \`$PRG --help' for more information."
1403                     exit 1
1404                 fi
1405                 ;;
1406             -C|--config)
1407                 if [ -n "$2" ] ; then
1408                     CONF="$2"
1409                     bootconf="$2"
1410                     ERR=" Error in $CONF:"
1411                     ARGS="$ARGS $1 $2"
1412                     shift 2
1413                 else
1414                     echo 1>&2 "$PRG: option requires an argument $1"
1415                     echo 1>&2 "Try \`$PRG --help' for more information."
1416                     exit 1
1417                 fi
1418                 ;;
1419             -m|--magicboot)
1420                 if [ -n "$2" ] ; then
1421                     magicboot="$2"
1422                     ARGWP=1
1423                     ARGS="$ARGS $1 $2"
1424                     shift 2
1425                 else
1426                     echo 1>&2 "$PRG: option requires an argument $1"
1427                     echo 1>&2 "Try \`$PRG --help' for more information."
1428                     exit 1
1429                 fi
1430                 ;;
1431             --filesystem)
1432                 if [ -n "$2" ] ; then
1433                     fstype="$2"
1434                     ARGFS=1
1435                     ARGS="$ARGS $1 $2"
1436                     shift 2
1437                 else
1438                     echo 1>&2 "$PRG: option requires an argument $1"
1439                     echo 1>&2 "Try \`$PRG --help' for more information."
1440                     exit 1
1441                 fi
1442                 ;;
1443             --nobless)
1444                 bless=no
1445                 ARGBS=1
1446                 ARGS="$ARGS $1"
1447                 shift
1448                 ;;
1449             -M|--mount)
1450                 usemount=yes
1451                 ARGMT=1
1452                 ARGS="$ARGS $1"
1453                 shift
1454                 ;;
1455             --protect)
1456                 protect=yes
1457                 ARGPT=1
1458                 ARGS="$ARGS $1"
1459                 shift
1460                 ;;
1461             --hide)
1462                 hide=yes
1463                 ARGHD=1
1464                 ARGS="$ARGS $1"
1465                 shift
1466                 ;;
1467             --nonvram)
1468                 nonvram=1
1469                 ARGNV=1
1470                 ARGS="$ARGS $1"
1471                 shift
1472                 ;;
1473             --device)
1474                 if [ -n "$2" ] ; then
1475                     device="$2"
1476                     bootconf=auto
1477                     echo 1>&2 "$PRG: WARNING: Deprecated option --device"
1478                     ARGS="$ARGS $1 $2"
1479                     shift 2
1480                 else
1481                     echo 1>&2 "$PRG: option requires an argument $1"
1482                     echo 1>&2 "Try \`$PRG --help' for more information."
1483                     exit 1
1484                 fi
1485                 ;;
1486             --timeout)
1487                 if [ -n "$2" ] ; then
1488                     timeout="$2"
1489                     bootconf=auto
1490                     echo 1>&2 "$PRG: WARNING: Deprecated option --device"
1491                     ARGS="$ARGS $1 $2"
1492                     shift 2
1493                 else
1494                     echo 1>&2 "$PRG: option requires an argument $1"
1495                     echo 1>&2 "Try \`$PRG --help' for more information."
1496                     exit 1
1497                 fi
1498                 ;;
1499             --image)
1500                 if [ -n "$2" ] ; then
1501                     image="$2"
1502                     bootconf=auto
1503                     echo 1>&2 "$PRG: WARNING: Deprecated option --device"
1504                     ARGS="$ARGS $1 $2"
1505                     shift 2
1506                 else
1507                     echo 1>&2 "$PRG: option requires an argument $1"
1508                     echo 1>&2 "Try \`$PRG --help' for more information."
1509                     exit 1
1510                 fi
1511                 ;;
1512             --label)
1513                 if [ -n "$2" ] ; then
1514                     label="$2"
1515                     bootconf=auto
1516                     echo 1>&2 "$PRG: WARNING: Deprecated option --device"
1517                     ARGS="$ARGS $1 $2"
1518                     shift 2
1519                 else
1520                     echo 1>&2 "$PRG: option requires an argument $1"
1521                     echo 1>&2 "Try \`$PRG --help' for more information."
1522                     exit 1
1523                 fi
1524                 ;;
1525             --partition)
1526                 if [ -n "$2" ] ; then
1527                     partition="$2"
1528                     bootconf=auto
1529                     echo 1>&2 "$PRG: WARNING: Deprecated option --device"
1530                     ARGS="$ARGS $1 $2"
1531                     shift 2
1532                 else
1533                     echo 1>&2 "$PRG: option requires an argument $1"
1534                     echo 1>&2 "Try \`$PRG --help' for more information."
1535                     exit 1
1536                 fi
1537                 ;;
1538             --root)
1539                 if [ -n "$2" ] ; then
1540                     root="$2"
1541                     bootconf=auto
1542                     echo 1>&2 "$PRG: WARNING: Deprecated option --device"
1543                     ARGS="$ARGS $1 $2"
1544                     shift 2
1545                 else
1546                     echo 1>&2 "$PRG: option requires an argument $1"
1547                     echo 1>&2 "Try \`$PRG --help' for more information."
1548                     exit 1
1549                 fi
1550                 ;;
1551             "")
1552                 break
1553                 ;;
1554             *)
1555                 echo 1>&2 "$PRG: unrecognized option \`$1'"
1556                 echo 1>&2 "Try \`$PRG --help' for more information."
1557                 exit 1
1558                 ;;
1559         esac
1560     done
1561 fi
1562
1563 ## check that specified config file exists, unless its /dev/null in
1564 ## which case we assume all options are done on the command line.
1565 if [ "$CONF" = /dev/null ] ; then
1566     true
1567 else
1568     confexist || exit 1
1569 fi
1570
1571 ## if there is no config file use the automatic generation to make a
1572 ## generic yaboot.conf. do this before the confcheck to avoid wierd errors.
1573 if [ "$bootconf" = /dev/null ] ; then
1574     if (command -v yabootconfig > /dev/null 2>&1) ; then
1575         echo 1>&2 "$PRG: Warning: no /etc/yaboot.conf, running yabootconfig to make one"
1576         yabootconfig --noinstall --quiet
1577         if [ $? != 0 ] ; then
1578             echo 1>&2 "$PRG: yabootconfig failed, please supply a valid /etc/yaboot.conf"
1579             echo 1>&2 "$PRG: You may also use $PRG's --boot, --image, --partition, and --device switches"
1580             echo 1>&2 "$PRG: These switches will cause $PRG to generate a basic yaboot.conf on the fly"
1581             exit 1
1582         else
1583             CONF=/etc/yaboot.conf
1584             bootconf=$CONF
1585             ERR=" Error in $CONF:"
1586             confexist || exit 1
1587         fi
1588     fi
1589 fi
1590
1591 ## Checks if each option was defined on the command line, and if so
1592 ## don't read it from the configuration file. this avoids
1593 ## configuration options from being set null, as well as command line
1594 ## options from being clobbered.
1595 [ "$ARGBT" != 1 -a $(parseconf ck boot) = 0 ] && boot=`parseconf str boot`
1596 [ "$ARGOB" != 1 -a $(parseconf ck ofboot) = 0 ] && ofboot=`parseconf str ofboot`
1597 [ "$ARGBF" != 1 -a $(parseconf ck install) = 0 ] && install=`parseconf str install`
1598 [ "$ARGWP" != 1 -a $(parseconf ck magicboot) = 0 ] && magicboot=`parseconf str magicboot`
1599 [ "$ARGMT" != 1 -a $(parseconf flag usemount) = 0 ] && usemount=yes
1600 [ "$ARGFS" != 1 -a $(parseconf ck fstype) = 0 ] && fstype=`parseconf str fstype`
1601 [ "$ARGBS" != 1 -a $(parseconf flag nobless) = 0 ] && bless=no
1602 [ "$ARGPT" != 1 -a $(parseconf flag protect) = 0 ] && protect=yes
1603 [ "$ARGHD" != 1 -a $(parseconf flag hide) = 0 ] && hide=yes
1604 [ "$ARGNV" != 1 -a $(parseconf flag nonvram) = 0 ] && nonvram=1
1605 [ $(parseconf ck hfstype) = 0 ] && hfstype=`parseconf str hfstype`
1606 [ $(parseconf ck hfscreator) = 0 ] && hfscreator=`parseconf str hfscreator`
1607 [ $(parseconf ck mntpoint) = 0 ] && mntpoint=`parseconf str mntpoint`
1608 [ $(parseconf ck delay) = 0 ] && delay=`parseconf str delay`
1609 [ $(parseconf ck timeout) = 0 ] && timeout=`parseconf str timeout`
1610 [ $(parseconf ck bsd) = 0 ] && bsd=`parseconf str bsd`
1611 [ $(parseconf ck macos) = 0 ] && macos=`parseconf str macos`
1612 [ $(parseconf ck macosx) = 0 ] && macosx=`parseconf str macosx`
1613 [ $(parseconf ck darwin) = 0 ] && darwin=`parseconf str darwin`
1614 [ $(parseconf ck defaultos) = 0 ] && defaultos=`parseconf str defaultos`
1615 [ $(parseconf ck fgcolor) = 0 ] && fgcolor=`parseconf str fgcolor`
1616 [ $(parseconf ck bgcolor) = 0 ] && bgcolor=`parseconf str bgcolor`
1617 [ $(parseconf ck icon) = 0 ] && export YBINOFICON=`parseconf str icon`
1618 [ $(parseconf flag enablecdboot) = 0 ] && cdrom=yes
1619 [ $(parseconf flag enablenetboot) = 0 ] && network=yes
1620 [ $(parseconf flag enableofboot) = 0 ] && of=yes
1621 [ $(parseconf flag brokenosx) = 0 ] && brokenosx=yes
1622
1623 bootparts=0
1624 for i in $boot; do
1625     bootparts=$(($bootparts + 1))
1626 done
1627 if [ "$bootparts" -gt 1 ]; then
1628     [ "$VERBOSE" = 1 ] && echo "$PRG: Iterating through list of boot partitions..."
1629     rc=0
1630     for i in $boot; do
1631         [ "$VERBOSE" = 1 ] && echo "$ABSPRG $ARGS -b $i"
1632         $ABSPRG $ARGS -b $i || rc=$?
1633     done
1634     exit $rc
1635 fi
1636
1637 ## ffs!! rtfm! foad!
1638 if [ "$boot" = unconfigured ] ; then
1639     echo 1>&2 "$PRG: You must specify the device for the bootstrap partition. (ie: boot=/dev/hdaX)"
1640     echo 1>&2 "$PRG: Try \`$PRG --help' for more information."
1641     exit 1
1642 fi
1643
1644 ## if there is still no config file use the automatic generation to make a
1645 ## generic yaboot.conf. do this before the confcheck to avoid wierd errors.
1646 if [ "$bootconf" = /dev/null ] ; then
1647     echo 1>&2 "$PRG: Warning: no yaboot.conf, using generic configuration."
1648     bootconf=auto
1649 fi
1650
1651 ## mntpoint is incompatible with mkofboot.
1652 if [ "$PRG" = mkofboot -a -n "$mntpoint" ] ; then
1653     echo 1>&2 "$PRG: Cannot be used with \`mntpoint='"
1654     exit 1
1655 fi
1656
1657 ## validate configuration for sanity.
1658 checkconf || exit 1
1659
1660 if [ "x$bootonce" != "x" ]; then
1661     foundlabel=`sed s/\#.*// $bootconf | grep "label=$bootonce$" | wc -l`
1662     if [ "$nonvram" = 1 ]; then
1663         echo 1>&2 "$PRG: --bootonce specified, but nvsetenv not available."
1664         exit 1
1665     fi
1666     if [ "$foundlabel" = 1 ]; then
1667         nvsetenv boot-once "$bootonce"
1668         foundlabel=`nvsetenv boot-once`
1669         if [ "$foundlabel" != "boot-once=$bootonce" -a \
1670              "$foundlabel" != "$bootonce" ]; then
1671             echo 1>&2 "$PRG: Could not nvsetenv boot-once $bootonce"
1672             exit 1
1673         fi
1674         [ "$VERBOSE" = 1 ] && echo "$PRG: nvsetenv boot-once $bootonce"
1675     else
1676         echo 1>&2 "$PRG: Could not find bootonce label [$bootonce] in $bootconf"
1677         exit 1
1678     fi
1679 fi
1680
1681 bootparts=0
1682
1683 ## check that we can use ofpath, its only needed for magicboot script
1684 ## building and nvram updates.
1685 if [ -n "$magicboot" -o "$nonvram" = 0 ] ; then
1686     if [ -z "$ofboot" -o -n "$macos" -o -n "$macosx" -o -n "$darwin" ] ; then
1687         if (command -v ofpath > /dev/null 2>&1) ; then
1688             [ -x `command -v ofpath` ]
1689             if [ $? != 0 ] ; then
1690                 echo 1>&2 "$PRG: ofpath could not be found, aborting."
1691                 exit 1
1692             fi
1693         else
1694             echo 1>&2 "$PRG: ofpath could not be found, aborting."
1695             exit 1
1696         fi
1697     fi
1698 fi
1699
1700 ## if password is set in yaboot.conf make sure permissions on that
1701 ## file are safe, warn if not.
1702 if (grep -q '^[[:space:]]*password[[:space:]]*=' "$bootconf" > /dev/null 2>&1) ; then
1703     permcheck
1704 fi
1705
1706 ## check if we are root if needed.
1707 if [ "$usemount" = yes -a -z "$mntpoint" ] ; then
1708     if [ `id -u` != 0 ] ; then
1709         echo 1>&2 "$PRG: \`usemount' requires root privileges, go away."
1710         exit 1
1711     fi
1712 fi
1713
1714 if [ "$fstype" = hfs ] ; then
1715     checkhfsutils
1716     if [ $? != 0 ] ; then
1717         echo 1>&2 "$PRG: hfsutils is not installed or cannot be found"
1718         echo 1>&2 "$PRG: Try --mount if `uname -sr` supports HFS"
1719         exit 1
1720     fi
1721 fi
1722
1723 ## convert unix device nodes to OpenFirmware pathnames
1724 if [ -n "$magicboot" -o "$nonvram" = 0 ] ; then
1725     convertpath || exit 1
1726 fi
1727
1728 ## yaboot.conf autogeneration. MUST have secure mktemp to
1729 ## avoid race conditions. Debian's mktemp qualifies.
1730 if [ "$bootconf" = auto ] ; then
1731     TMPCONF=`mktemp -q "$TMP/$PRG.XXXXXX"`
1732     if [ $? != 0 ] ; then
1733         echo 1>&2 "$PRG: Could not create temporary file, aborting."
1734         exit 1
1735     fi
1736
1737     mkconf
1738     if [ $? != 0 ] ; then
1739         echo 1>&2 "$PRG: An error occured generating yaboot.conf, aborting."
1740         exit 1
1741     fi
1742
1743     bootconf="$TMPCONF"
1744 fi
1745
1746 if [ -n "$magicboot" ] ; then
1747     checkfirststage || exit 1
1748     if [ "$FIRSTSTG" = compat ] ; then
1749         mkfirststage
1750         if [ $? != 0 ] ; then
1751             echo 1>&2 "$PRG: An error occured while building first stage loader.  aborting..."
1752             exit 1
1753         fi
1754     fi
1755 fi
1756
1757 case "$PRG" in
1758     ybin)
1759         case "$usemount" in
1760             no)
1761                 if [ "$fstype" = raw ] ; then
1762                     luserck || exit 1
1763                     raw_install || exit 1
1764                 else
1765                     util_install || exit 1
1766                 fi
1767                 exit 0
1768                 ;;
1769             yes)
1770                 mnt_install || exit 1
1771                 exit 0
1772                 ;;
1773         esac
1774         ;;
1775     mkofboot)
1776         case "$usemount" in
1777             no)
1778                 ## its not nice to erase the partition and then bail!
1779                 if [ "$fstype" = msdos ] ; then
1780                     echo 1>&2 "$PRG: mtools support is not implemented"
1781                     echo 1>&2 "$PRG: Use --mount or add \`usemount' to $CONF"
1782                     exit 1
1783                 fi
1784                 confirm || exit 2
1785                 if [ "$fstype" = raw ] ; then
1786                     raw_install || exit 1
1787                 else
1788                     mkoffs || exit 1
1789                     util_install || exit 1
1790                 fi
1791                 [ "$VERBOSE" = 1 ] && echo "$PRG: Installation complete."
1792                 exit 0
1793                 ;;
1794             yes)
1795                 confirm || exit 2
1796                 mkoffs || exit 1
1797                 mnt_install || exit 1
1798                 [ "$VERBOSE" = 1 ] && echo "$PRG: Installation complete."
1799                 exit 0
1800                 ;;
1801         esac
1802         ;;
1803 esac
1804
1805 exit 0