]> git.ozlabs.org Git - yaboot.git/blob - ybin/ybin
Force build to be ppc32
[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.14
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 }
1156
1157 ## make sure the first stage ofboot generator is compatible.
1158 checkfirststage()
1159 {
1160     grep -q "^#%ybinscript-" "$magicboot" 2> /dev/null
1161     if [ "$?" = 0 ] ; then
1162         local magic=`grep "^#%ybinscript-" "$magicboot"`
1163         local ver="${magic##*-}"
1164         if [ "$ver" = "1.1" ] ; then
1165             FIRSTSTG=compat
1166             return 0
1167         else
1168             echo 1>&2 "$PRG: Incompatible version of first stage loader $magicboot.  aborting..."
1169             return 1
1170         fi
1171     else
1172         FIRSTSTG=old
1173         return 0
1174     fi
1175 }
1176
1177 ## build the first stage loader.
1178 mkfirststage()
1179 {
1180     ## must have 7 backslashes to == \\ printf + shell = bizarre... or,
1181     ## make special variable to contain a \ (need \\ to make \) to work
1182     ## around echo -e -n brokeness.
1183     local BS='\\'
1184     local OS=1
1185
1186     ## deal with mntpoint=
1187     [ -n "$OFDIR" ] && local OFDIR="${BS}${OFDIR}${BS}"
1188
1189     ## some misguided people insist on installing OSX on
1190     ## HorribleFileSystem+ instead of UFS, as a result MacOS deblesses
1191     ## OSX, making it unbootable. if apple localizes the filesystem hierarchy again screw it.
1192     [ "$brokenosx" = yes ] && local OSXBOOT="${BS}System${BS}Library${BS}CoreServices${BS}BootX"
1193     [ "$brokenosx" = no ] && local OSXBOOT="${BS}${BS}:tbxi"
1194
1195     ## assign variables for configured menu options.
1196     [ "$usemount" = no -a "$bless" = yes ] && local YB="yaboot GNU l $ofboot ,${BS}${BS}yaboot"
1197     [ "$usemount" = yes -o "$bless" = no ] && local YB="yaboot GNU l $ofboot ,${OFDIR}yaboot"
1198     [ -n "$bsd" ] && OS="$(($OS + 1))" && local BSD="ybsd BSD b $ofboot ,${BS}${BS}ofwboot/$bsd"
1199     [ -n "$macos" ] && OS="$(($OS + 1))" && local MAC="macos MacOS m $macos ,${BS}${BS}:tbxi"
1200     [ -n "$macosx" ] && OS="$(($OS + 1))" && local MX="macosx MacOSX x $macosx ,${OSXBOOT}"
1201     [ -n "$darwin" ] && OS="$(($OS + 1))" && local DW="darwin Darwin d $darwin ,${BS}${BS}:tbxi"
1202     [ "$cdrom" = yes ] && OS="$(($OS + 1))" && local CD="cd CDROM c cd: ,${BS}${BS}:tbxi"
1203     [ "$network" = yes ] && OS="$(($OS + 1))" && local NET="net Network n enet: 0"
1204     [ "$of" = yes ] && OS="$(($OS + 1))" && local OF="of OpenFirmware o quit now"
1205     [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: OS=$OS"
1206
1207     ## call ofboot,
1208     ## Usage: OS-count defaultos timeout fgc bgc osname oslabel oskey osdev osfile ...
1209     [ "$DEBUG" = 1 ] && $PRINTF 1>&2 "$PRG: DEBUG: /bin/sh $magicboot $OS $defaultos $delay $fbc $bgc $YB $BSD $MAC $MX $DW $CD $NET $OF\n"
1210     FIRST="$(/bin/sh "$magicboot" "$OS" "$defaultos" "$delay" $fgc $bgc ${YB} ${BSD} ${MAC} ${MX} ${DW} ${CD} ${NET} ${OF})" || return 1
1211
1212     return 0
1213 }
1214
1215 ## mkofboot function.
1216 mkoffs()
1217 {
1218     mount | grep "^$boot\>" > /dev/null
1219     if [ $? = 0 ] ; then
1220         echo 1>&2 "$PRG: $boot appears to be mounted! aborting."
1221         return 1
1222     fi
1223
1224     case "$fstype" in
1225         hfs)
1226             [ "$VERBOSE" = 1 ] && echo "$PRG: Creating HFS filesystem on $boot..."
1227             if (command -v dd > /dev/null 2>&1) ; then
1228                 dd if=/dev/zero of="$boot" bs=512 count=1600 > /dev/null 2>&1
1229             fi
1230             hformat -l bootstrap "$boot" > /dev/null
1231             if [ $? != 0 ] ; then
1232                 echo 1>&2 "$PRG: HFS filesystem creation failed!"
1233                 return 1
1234             fi
1235             humount "$boot" ## otherwise we might get confused.
1236             return 0
1237             ;;
1238         msdos)
1239             if (command -v mkdosfs > /dev/null 2>&1) ; then
1240                 [ -x `command -v mkdosfs` ] || FAIL=1 ; else FAIL=1 ; fi
1241                 if [ "$FAIL" = 1 ] ; then
1242                     echo 1>&2 "$PRG: mkdosfs is not installed or cannot be found"
1243                     return 1
1244                 fi
1245
1246             [ "$VERBOSE" = 1 ] && echo "$PRG: Creating DOS filesystem on $boot..."
1247             if (command -v dd > /dev/null 2>&1) ; then
1248                 dd if=/dev/zero of="$boot" bs=512 count=1600 > /dev/null 2>&1
1249             fi
1250             mkdosfs -n bootstrap "$boot" > /dev/null
1251             if [ $? != 0 ] ; then
1252                 echo 1>&2 "$PRG: DOS filesystem creation failed!"
1253                 return 1
1254             fi
1255             return 0
1256             ;;
1257     esac
1258 }
1259
1260 confirm()
1261 {
1262     if [ "$FORCE" = yes ] ; then
1263         return 0
1264     else
1265         echo 1>&2
1266         [ "$fstype" = raw ] && $PRINTF 1>&2 "$PRG: Overwrite contents of $boot with $install? [y/N] "
1267         [ "$fstype" != raw ] && $PRINTF 1>&2 "$PRG: Create $fstype filesystem on $boot? [y/N] "
1268         read ans
1269         case "$ans" in
1270             y|Y)
1271                 return 0
1272                 ;;
1273             *)
1274                 echo 1>&2 "$PRG: Abort."
1275                 return 2
1276                 ;;
1277         esac
1278     fi
1279 }
1280
1281 ## for fstype=raw check if an ELF binary has already been dded.
1282 luserck()
1283 {
1284     if [ "$(dd if="$boot" bs=1 skip=1 count=3 2>/dev/null)" = ELF ] ; then
1285         return 0
1286     else
1287         echo 1>&2 "$PRG: This partition has never had yaboot installed before, please run mkofboot"
1288         return 1
1289     fi
1290 }
1291
1292 mkconf()
1293 {
1294 ## defaults for this are defined at the beginning of the script with
1295 ## other variables.
1296
1297 echo \
1298 "## yaboot configuration file generated by ybin $VERSION
1299
1300 device=$device
1301 timeout=$timeout
1302
1303 image=$image
1304         label=$label
1305         partition=$partition
1306         root=$root
1307         read-only
1308 " > "$TMPCONF" || return 1
1309
1310 [ "$DEBUG" = 1 ] && $PRINTF 1>&2 "\nDEBUG: autoconf:\n----\n" && cat "$TMPCONF" 1>&2 && echo 1>&2 "----"
1311 return 0
1312 }
1313
1314 ## take out the trash.
1315 cleanup()
1316 {
1317     if [ -n "$TMPCONF" ] ; then rm -f "$TMPCONF" ; fi
1318     if [ -n "$FIRST" ] ; then rm -f "$FIRST" ; fi
1319     if [ -n "$TMPYABOOT" ] ; then rm -f "$TMPYABOOT" ; fi
1320     if [ -d "$TMP/bootstrap.$$" -a "$usemount" = yes ] ; then rmdir "$TMP/bootstrap.$$" ; fi
1321     return 0
1322 }
1323
1324 ##########
1325 ## Main ##
1326 ##########
1327
1328 ## absurdly bloated case statement to parse command line options.
1329 if [ $# != 0 ] ; then
1330     while true ; do
1331         case "$1" in
1332             -V|--version)
1333                 version
1334                 exit 0
1335                 ;;
1336             -h|--help)
1337                 usage
1338                 exit 0
1339                 ;;
1340             --debug)
1341                 DEBUG=1
1342                 ARGS="$ARGS $1"
1343                 shift
1344                 ;;
1345             -v|--verbose)
1346                 VERBOSE=1
1347                 ARGS="$ARGS $1"
1348                 shift
1349                 ;;
1350             -f|--force)
1351                 FORCE=yes
1352                 ARGS="$ARGS $1"
1353                 shift
1354                 ;;
1355             -b|--boot)
1356                 if [ -n "$2" ] ; then
1357                     if [ "$boot" = "unconfigured" ]; then
1358                         boot="$2"
1359                     else
1360                         boot="$boot $2"
1361                     fi
1362                     ARGBT=1
1363                     shift 2
1364                 else
1365                     echo 1>&2 "$PRG: option requires an argument $1"
1366                     echo 1>&2 "Try \`$PRG --help' for more information."
1367                     exit 1
1368                 fi
1369                 ;;
1370             -o|--ofboot)
1371                 if [ -n "$2" ] ; then
1372                     ofboot="$2"
1373                     ARGOB=1
1374                     ARGS="$ARGS $1 $2"
1375                     shift 2
1376                 else
1377                     echo 1>&2 "$PRG: option requires an argument $1"
1378                     echo 1>&2 "Try \`$PRG --help' for more information."
1379                     exit 1
1380                 fi
1381                 ;;
1382             --bootonce)
1383                 if [ -n "$2" ] ; then
1384                     bootonce="$2"
1385                     ARGS="$ARGS $1 $2"
1386                     shift 2
1387                 else
1388                     echo 1>&2 "$PRG: option requires an argument $1"
1389                     echo 1>&2 "Try \`$PRG --help' for more information."
1390                     exit 1
1391                 fi
1392                 ;;
1393             -i|--install)
1394                 if [ -n "$2" ] ; then
1395                     install="$2"
1396                     ARGBF=1
1397                     ARGS="$ARGS $1 $2"
1398                     shift 2
1399                 else
1400                     echo 1>&2 "$PRG: option requires an argument $1"
1401                     echo 1>&2 "Try \`$PRG --help' for more information."
1402                     exit 1
1403                 fi
1404                 ;;
1405             -C|--config)
1406                 if [ -n "$2" ] ; then
1407                     CONF="$2"
1408                     bootconf="$2"
1409                     ERR=" Error in $CONF:"
1410                     ARGS="$ARGS $1 $2"
1411                     shift 2
1412                 else
1413                     echo 1>&2 "$PRG: option requires an argument $1"
1414                     echo 1>&2 "Try \`$PRG --help' for more information."
1415                     exit 1
1416                 fi
1417                 ;;
1418             -m|--magicboot)
1419                 if [ -n "$2" ] ; then
1420                     magicboot="$2"
1421                     ARGWP=1
1422                     ARGS="$ARGS $1 $2"
1423                     shift 2
1424                 else
1425                     echo 1>&2 "$PRG: option requires an argument $1"
1426                     echo 1>&2 "Try \`$PRG --help' for more information."
1427                     exit 1
1428                 fi
1429                 ;;
1430             --filesystem)
1431                 if [ -n "$2" ] ; then
1432                     fstype="$2"
1433                     ARGFS=1
1434                     ARGS="$ARGS $1 $2"
1435                     shift 2
1436                 else
1437                     echo 1>&2 "$PRG: option requires an argument $1"
1438                     echo 1>&2 "Try \`$PRG --help' for more information."
1439                     exit 1
1440                 fi
1441                 ;;
1442             --nobless)
1443                 bless=no
1444                 ARGBS=1
1445                 ARGS="$ARGS $1"
1446                 shift
1447                 ;;
1448             -M|--mount)
1449                 usemount=yes
1450                 ARGMT=1
1451                 ARGS="$ARGS $1"
1452                 shift
1453                 ;;
1454             --protect)
1455                 protect=yes
1456                 ARGPT=1
1457                 ARGS="$ARGS $1"
1458                 shift
1459                 ;;
1460             --hide)
1461                 hide=yes
1462                 ARGHD=1
1463                 ARGS="$ARGS $1"
1464                 shift
1465                 ;;
1466             --nonvram)
1467                 nonvram=1
1468                 ARGNV=1
1469                 ARGS="$ARGS $1"
1470                 shift
1471                 ;;
1472             --device)
1473                 if [ -n "$2" ] ; then
1474                     device="$2"
1475                     bootconf=auto
1476                     echo 1>&2 "$PRG: WARNING: Deprecated option --device"
1477                     ARGS="$ARGS $1 $2"
1478                     shift 2
1479                 else
1480                     echo 1>&2 "$PRG: option requires an argument $1"
1481                     echo 1>&2 "Try \`$PRG --help' for more information."
1482                     exit 1
1483                 fi
1484                 ;;
1485             --timeout)
1486                 if [ -n "$2" ] ; then
1487                     timeout="$2"
1488                     bootconf=auto
1489                     echo 1>&2 "$PRG: WARNING: Deprecated option --device"
1490                     ARGS="$ARGS $1 $2"
1491                     shift 2
1492                 else
1493                     echo 1>&2 "$PRG: option requires an argument $1"
1494                     echo 1>&2 "Try \`$PRG --help' for more information."
1495                     exit 1
1496                 fi
1497                 ;;
1498             --image)
1499                 if [ -n "$2" ] ; then
1500                     image="$2"
1501                     bootconf=auto
1502                     echo 1>&2 "$PRG: WARNING: Deprecated option --device"
1503                     ARGS="$ARGS $1 $2"
1504                     shift 2
1505                 else
1506                     echo 1>&2 "$PRG: option requires an argument $1"
1507                     echo 1>&2 "Try \`$PRG --help' for more information."
1508                     exit 1
1509                 fi
1510                 ;;
1511             --label)
1512                 if [ -n "$2" ] ; then
1513                     label="$2"
1514                     bootconf=auto
1515                     echo 1>&2 "$PRG: WARNING: Deprecated option --device"
1516                     ARGS="$ARGS $1 $2"
1517                     shift 2
1518                 else
1519                     echo 1>&2 "$PRG: option requires an argument $1"
1520                     echo 1>&2 "Try \`$PRG --help' for more information."
1521                     exit 1
1522                 fi
1523                 ;;
1524             --partition)
1525                 if [ -n "$2" ] ; then
1526                     partition="$2"
1527                     bootconf=auto
1528                     echo 1>&2 "$PRG: WARNING: Deprecated option --device"
1529                     ARGS="$ARGS $1 $2"
1530                     shift 2
1531                 else
1532                     echo 1>&2 "$PRG: option requires an argument $1"
1533                     echo 1>&2 "Try \`$PRG --help' for more information."
1534                     exit 1
1535                 fi
1536                 ;;
1537             --root)
1538                 if [ -n "$2" ] ; then
1539                     root="$2"
1540                     bootconf=auto
1541                     echo 1>&2 "$PRG: WARNING: Deprecated option --device"
1542                     ARGS="$ARGS $1 $2"
1543                     shift 2
1544                 else
1545                     echo 1>&2 "$PRG: option requires an argument $1"
1546                     echo 1>&2 "Try \`$PRG --help' for more information."
1547                     exit 1
1548                 fi
1549                 ;;
1550             "")
1551                 break
1552                 ;;
1553             *)
1554                 echo 1>&2 "$PRG: unrecognized option \`$1'"
1555                 echo 1>&2 "Try \`$PRG --help' for more information."
1556                 exit 1
1557                 ;;
1558         esac
1559     done
1560 fi
1561
1562 ## check that specified config file exists, unless its /dev/null in
1563 ## which case we assume all options are done on the command line.
1564 if [ "$CONF" = /dev/null ] ; then
1565     true
1566 else
1567     confexist || exit 1
1568 fi
1569
1570 ## if there is no config file use the automatic generation to make a
1571 ## generic yaboot.conf. do this before the confcheck to avoid wierd errors.
1572 if [ "$bootconf" = /dev/null ] ; then
1573     if (command -v yabootconfig > /dev/null 2>&1) ; then
1574         echo 1>&2 "$PRG: Warning: no /etc/yaboot.conf, running yabootconfig to make one"
1575         yabootconfig --noinstall --quiet
1576         if [ $? != 0 ] ; then
1577             echo 1>&2 "$PRG: yabootconfig failed, please supply a valid /etc/yaboot.conf"
1578             echo 1>&2 "$PRG: You may also use $PRG's --boot, --image, --partition, and --device switches"
1579             echo 1>&2 "$PRG: These switches will cause $PRG to generate a basic yaboot.conf on the fly"
1580             exit 1
1581         else
1582             CONF=/etc/yaboot.conf
1583             bootconf=$CONF
1584             ERR=" Error in $CONF:"
1585             confexist || exit 1
1586         fi
1587     fi
1588 fi
1589
1590 ## Checks if each option was defined on the command line, and if so
1591 ## don't read it from the configuration file. this avoids
1592 ## configuration options from being set null, as well as command line
1593 ## options from being clobbered.
1594 [ "$ARGBT" != 1 -a $(parseconf ck boot) = 0 ] && boot=`parseconf str boot`
1595 [ "$ARGOB" != 1 -a $(parseconf ck ofboot) = 0 ] && ofboot=`parseconf str ofboot`
1596 [ "$ARGBF" != 1 -a $(parseconf ck install) = 0 ] && install=`parseconf str install`
1597 [ "$ARGWP" != 1 -a $(parseconf ck magicboot) = 0 ] && magicboot=`parseconf str magicboot`
1598 [ "$ARGMT" != 1 -a $(parseconf flag usemount) = 0 ] && usemount=yes
1599 [ "$ARGFS" != 1 -a $(parseconf ck fstype) = 0 ] && fstype=`parseconf str fstype`
1600 [ "$ARGBS" != 1 -a $(parseconf flag nobless) = 0 ] && bless=no
1601 [ "$ARGPT" != 1 -a $(parseconf flag protect) = 0 ] && protect=yes
1602 [ "$ARGHD" != 1 -a $(parseconf flag hide) = 0 ] && hide=yes
1603 [ "$ARGNV" != 1 -a $(parseconf flag nonvram) = 0 ] && nonvram=1
1604 [ $(parseconf ck hfstype) = 0 ] && hfstype=`parseconf str hfstype`
1605 [ $(parseconf ck hfscreator) = 0 ] && hfscreator=`parseconf str hfscreator`
1606 [ $(parseconf ck mntpoint) = 0 ] && mntpoint=`parseconf str mntpoint`
1607 [ $(parseconf ck delay) = 0 ] && delay=`parseconf str delay`
1608 [ $(parseconf ck timeout) = 0 ] && timeout=`parseconf str timeout`
1609 [ $(parseconf ck bsd) = 0 ] && bsd=`parseconf str bsd`
1610 [ $(parseconf ck macos) = 0 ] && macos=`parseconf str macos`
1611 [ $(parseconf ck macosx) = 0 ] && macosx=`parseconf str macosx`
1612 [ $(parseconf ck darwin) = 0 ] && darwin=`parseconf str darwin`
1613 [ $(parseconf ck defaultos) = 0 ] && defaultos=`parseconf str defaultos`
1614 [ $(parseconf ck fgcolor) = 0 ] && fgcolor=`parseconf str fgcolor`
1615 [ $(parseconf ck bgcolor) = 0 ] && bgcolor=`parseconf str bgcolor`
1616 [ $(parseconf ck icon) = 0 ] && export YBINOFICON=`parseconf str icon`
1617 [ $(parseconf flag enablecdboot) = 0 ] && cdrom=yes
1618 [ $(parseconf flag enablenetboot) = 0 ] && network=yes
1619 [ $(parseconf flag enableofboot) = 0 ] && of=yes
1620 [ $(parseconf flag brokenosx) = 0 ] && brokenosx=yes
1621
1622 bootparts=0
1623 for i in $boot; do
1624     bootparts=$(($bootparts + 1))
1625 done
1626 if [ "$bootparts" -gt 1 ]; then
1627     [ "$VERBOSE" = 1 ] && echo "$PRG: Iterating through list of boot partitions..."
1628     rc=0
1629     for i in $boot; do
1630         [ "$VERBOSE" = 1 ] && echo "$ABSPRG $ARGS -b $i"
1631         $ABSPRG $ARGS -b $i || rc=$?
1632     done
1633     exit $rc
1634 fi
1635
1636 ## ffs!! rtfm! foad!
1637 if [ "$boot" = unconfigured ] ; then
1638     echo 1>&2 "$PRG: You must specify the device for the bootstrap partition. (ie: boot=/dev/hdaX)"
1639     echo 1>&2 "$PRG: Try \`$PRG --help' for more information."
1640     exit 1
1641 fi
1642
1643 ## if there is still no config file use the automatic generation to make a
1644 ## generic yaboot.conf. do this before the confcheck to avoid wierd errors.
1645 if [ "$bootconf" = /dev/null ] ; then
1646     echo 1>&2 "$PRG: Warning: no yaboot.conf, using generic configuration."
1647     bootconf=auto
1648 fi
1649
1650 ## mntpoint is incompatible with mkofboot.
1651 if [ "$PRG" = mkofboot -a -n "$mntpoint" ] ; then
1652     echo 1>&2 "$PRG: Cannot be used with \`mntpoint='"
1653     exit 1
1654 fi
1655
1656 ## validate configuration for sanity.
1657 checkconf || exit 1
1658
1659 if [ "x$bootonce" != "x" ]; then
1660     foundlabel=`sed s/\#.*// $bootconf | grep "label=$bootonce$" | wc -l`
1661     if [ "$nonvram" = 0 ]; then
1662         echo 1>&2 "$PRG: --bootonce specified, but nvsetenv not available."
1663         exit 1
1664     fi
1665     if [ "$foundlabel" = 1 ]; then
1666         nvsetenv boot-once "$bootonce"
1667         foundlabel=`nvsetenv boot-once`
1668         if [ "$foundlabel" != "boot-once=$bootonce" ]; then
1669             echo 1>&2 "$PRG: Could not nvsetenv boot-once $bootonce"
1670             exit 1
1671         fi
1672         [ "$VERBOSE" = 1 ] && echo "$PRG: nvsetenv boot-once $bootonce"
1673     else
1674         echo 1>&2 "$PRG: Could not find bootonce label [$bootonce] in $bootconf"
1675         exit 1
1676     fi
1677 fi
1678
1679 bootparts=0
1680
1681 ## check that we can use ofpath, its only needed for magicboot script
1682 ## building and nvram updates.
1683 if [ -n "$magicboot" -o "$nonvram" = 0 ] ; then
1684     if [ -z "$ofboot" -o -n "$macos" -o -n "$macosx" -o -n "$darwin" ] ; then
1685         if (command -v ofpath > /dev/null 2>&1) ; then
1686             [ -x `command -v ofpath` ]
1687             if [ $? != 0 ] ; then
1688                 echo 1>&2 "$PRG: ofpath could not be found, aborting."
1689                 exit 1
1690             fi
1691         else
1692             echo 1>&2 "$PRG: ofpath could not be found, aborting."
1693             exit 1
1694         fi
1695     fi
1696 fi
1697
1698 ## if password is set in yaboot.conf make sure permissions on that
1699 ## file are safe, warn if not.
1700 if (grep -q '^[[:space:]]*password[[:space:]]*=' "$bootconf" > /dev/null 2>&1) ; then
1701     permcheck
1702 fi
1703
1704 ## check if we are root if needed.
1705 if [ "$usemount" = yes -a -z "$mntpoint" ] ; then
1706     if [ `id -u` != 0 ] ; then
1707         echo 1>&2 "$PRG: \`usemount' requires root privileges, go away."
1708         exit 1
1709     fi
1710 fi
1711
1712 if [ "$fstype" = hfs ] ; then
1713     checkhfsutils
1714     if [ $? != 0 ] ; then
1715         echo 1>&2 "$PRG: hfsutils is not installed or cannot be found"
1716         echo 1>&2 "$PRG: Try --mount if `uname -sr` supports HFS"
1717         exit 1
1718     fi
1719 fi
1720
1721 ## convert unix device nodes to OpenFirmware pathnames
1722 if [ -n "$magicboot" -o "$nonvram" = 0 ] ; then
1723     convertpath || exit 1
1724 fi
1725
1726 ## yaboot.conf autogeneration. MUST have secure mktemp to
1727 ## avoid race conditions. Debian's mktemp qualifies.
1728 if [ "$bootconf" = auto ] ; then
1729     TMPCONF=`mktemp -q "$TMP/$PRG.XXXXXX"`
1730     if [ $? != 0 ] ; then
1731         echo 1>&2 "$PRG: Could not create temporary file, aborting."
1732         exit 1
1733     fi
1734
1735     mkconf
1736     if [ $? != 0 ] ; then
1737         echo 1>&2 "$PRG: An error occured generating yaboot.conf, aborting."
1738         exit 1
1739     fi
1740
1741     bootconf="$TMPCONF"
1742 fi
1743
1744 if [ -n "$magicboot" ] ; then
1745     checkfirststage || exit 1
1746     if [ "$FIRSTSTG" = compat ] ; then
1747         mkfirststage
1748         if [ $? != 0 ] ; then
1749             echo 1>&2 "$PRG: An error occured while building first stage loader.  aborting..."
1750             exit 1
1751         fi
1752     fi
1753 fi
1754
1755 case "$PRG" in
1756     ybin)
1757         case "$usemount" in
1758             no)
1759                 if [ "$fstype" = raw ] ; then
1760                     luserck || exit 1
1761                     raw_install || exit 1
1762                 else
1763                     util_install || exit 1
1764                 fi
1765                 exit 0
1766                 ;;
1767             yes)
1768                 mnt_install || exit 1
1769                 exit 0
1770                 ;;
1771         esac
1772         ;;
1773     mkofboot)
1774         case "$usemount" in
1775             no)
1776                 ## its not nice to erase the partition and then bail!
1777                 if [ "$fstype" = msdos ] ; then
1778                     echo 1>&2 "$PRG: mtools support is not implemented"
1779                     echo 1>&2 "$PRG: Use --mount or add \`usemount' to $CONF"
1780                     exit 1
1781                 fi
1782                 confirm || exit 2
1783                 if [ "$fstype" = raw ] ; then
1784                     raw_install || exit 1
1785                 else
1786                     mkoffs || exit 1
1787                     util_install || exit 1
1788                 fi
1789                 [ "$VERBOSE" = 1 ] && echo "$PRG: Installation complete."
1790                 exit 0
1791                 ;;
1792             yes)
1793                 confirm || exit 2
1794                 mkoffs || exit 1
1795                 mnt_install || exit 1
1796                 [ "$VERBOSE" = 1 ] && echo "$PRG: Installation complete."
1797                 exit 0
1798                 ;;
1799         esac
1800         ;;
1801 esac
1802
1803 exit 0