Fix to make making a bootable cd possible again.

ow that the image has grown beyond the 1.44M that fits on a floppy.
(previously, the floppy emulation mode was used for cd's.)

the boot cd now uses 'no emulation mode,' where an image is provided on
the cd that is loaded and executed directly. this is the boot monitor.

in order to make this work (the entry point is the same as where the
image is loaded, and the boot monitor needs its a.out header too) and
keep compatability with the same code being used for regular booting, i
prepended 16 bytes that jumps over its header so execution can start
there.

to be able to read the CD (mostly in order to read the boot image),
boot has to use the already present 'extended read' call, but address
the CD using 2k sectors.
This commit is contained in:
Ben Gras 2010-01-18 14:10:04 +00:00
parent 5efa92f754
commit daca9de450
8 changed files with 191 additions and 103 deletions

View file

@ -36,9 +36,16 @@ rawfs86.o: rawfs.c rawfs.o
-cmp -s rawfs.o rawfs86.o && ln -f rawfs.o rawfs86.o
boot: boothead.s boot.o bootimage.o rawfs86.o
$(LD86) -o $@ \
$(LD86) -o bootexec \
boothead.s boot.o bootimage.o rawfs86.o $(LIBS)
install -S 8kb boot
install -S 12kb bootexec
# This is code that is executed when used on a bootable
# CD, as its entry point is the start of the file then.
# It jumps over the a.out header into the part of the
# code in boothead.s where the code knows it's booting
# from CD if entered there.
( printf '\xeb\x3e ' ; cat bootexec ) >boot
chmod 755 boot
edparams.o: boot.c
ln -f boot.c edparams.c

View file

@ -122,6 +122,44 @@ char *bios_err(int err)
return "Unknown error";
}
/* CD's are addressed in 2048-byte sectors.
* In order to be able to read CD's but maintain the same interface of 512-byte
* sector addressing, we check if the device is a CD in readsectors() and if so,
* read it into our own buffer first
*/
int readsectors(u32_t bufaddr, u32_t sector, U8_t count)
{
#define CDSECTOR_SIZE 2048
static char cdbuf[CDSECTOR_SIZE];
static i32_t cdbuf_sec = -1;
i32_t cdsec;
if(device != cddevice) {
return biosreadsectors(bufaddr, sector, count);
}
while(count > 0) {
u32_t offset;
#define FACTOR (CDSECTOR_SIZE/SECTOR_SIZE)
cdsec = sector / FACTOR;
offset = (sector % FACTOR) * SECTOR_SIZE;
if(cdsec != cdbuf_sec) {
int r;
if((r=biosreadsectors(mon2abs(cdbuf), cdsec, 1)) != 0) {
printf("error %d\n", r);
return r;
}
cdbuf_sec = cdsec;
}
raw_copy(bufaddr, mon2abs(cdbuf) + offset, SECTOR_SIZE);
bufaddr += SECTOR_SIZE;
count--;
sector++;
}
return 0;
}
char *unix_err(int err)
/* Translate the few errors rawfs can give. */
{
@ -515,6 +553,12 @@ void initialize(void)
}
#endif
/* If we were booted from CD, remember what device it was. */
if(cdbooted)
cddevice = device;
else
cddevice = 0xff; /* Invalid. */
/* Set the new caddr for relocate. */
caddr= newaddr;
@ -564,6 +608,14 @@ void initialize(void)
readerr(masterpos, r); exit(1);
}
/* If we're a CD, we know what we want. */
if(device == cddevice) {
p = 1; /* We know this is the root FS. */
lowsec = table[p]->lowsec;
bootdev.primary = p;
break; /* Found! */
}
/* See if you can find "lowsec" back. */
for (p= 0; p < NR_PARTITIONS; p++) {
if (lowsec - table[p]->lowsec < table[p]->size) break;
@ -590,12 +642,17 @@ void initialize(void)
bootdev.primary= p;
masterpos= table[p]->lowsec;
}
strcpy(bootdev.name, "d0p0");
bootdev.name[1] += (device - 0x80);
bootdev.name[3] += bootdev.primary;
if (bootdev.secondary >= 0) {
strcat(bootdev.name, "s0");
bootdev.name[5] += bootdev.secondary;
if(device == cddevice) {
strcpy(bootdev.name, CDNAME);
} else {
strcpy(bootdev.name, "d0p0");
bootdev.name[1] += (device - 0x80);
bootdev.name[3] += bootdev.primary;
if (bootdev.secondary >= 0) {
strcat(bootdev.name, "s0");
bootdev.name[5] += bootdev.secondary;
}
}
/* Find out about the video hardware. */
@ -1087,7 +1144,7 @@ dev_t name2dev(char *name)
n= name;
if (strncmp(n, "/dev/", 5) == 0) n+= 5;
if (strcmp(n, "ram") == 0) {
if (strcmp(n, "ram") == 0 || strcmp(n, CDNAME) == 0) {
dev= DEV_RAM;
} else
if (n[0] == 'f' && n[1] == 'd' && numeric(n+2)) {
@ -1879,7 +1936,6 @@ void monitor(void)
void boot(void)
/* Load Minix and start it, among other things. */
{
/* Initialize tables. */
initialize();
@ -1928,8 +1984,7 @@ void main(int argc, char **argv)
fatal(bootdev.name);
/* Check if it is a bootable Minix device. */
if (readsectors(mon2abs(bootcode), lowsec, 1) != 0
|| memcmp(bootcode, boot_magic, sizeof(boot_magic)) != 0) {
if (readsectors(mon2abs(bootcode), lowsec, 1) != 0) {
fprintf(stderr, "edparams: %s: not a bootable Minix device\n",
bootdev.name);
exit(1);

View file

@ -58,6 +58,9 @@ EXTERN u32_t caddr, daddr; /* Code and data address of the boot program. */
EXTERN u32_t runsize; /* Size of this program. */
EXTERN u16_t device; /* Drive being booted from. */
EXTERN u16_t cddevice; /* Drive that is CD if known. */
#define CDNAME "cd" /* Name of the CD device. */
typedef struct { /* One chunk of free memory. */
u32_t base; /* Start byte. */
@ -66,6 +69,7 @@ typedef struct { /* One chunk of free memory. */
EXTERN memory mem[3]; /* List of available memory. */
EXTERN int mon_return; /* Monitor stays in memory? */
EXTERN int cdbooted; /* Did we boot from CD? (Set by boothead.s.) */
typedef struct bios_env
{
@ -101,6 +105,9 @@ int readsectors(u32_t bufaddr, u32_t sector, U8_t count);
/* Read 1 or more sectors from "device". */
int writesectors(u32_t bufaddr, u32_t sector, U8_t count);
/* Write 1 or more sectors to "device". */
int biosreadsectors(u32_t bufaddr, u32_t sector, U8_t count);
int getch(void);
/* Read a keypress. */
void scan_keyboard(void);

View file

@ -41,19 +41,30 @@
.extern _rem_part ! To pass partition info
.extern _k_flags ! Special kernel flags
.extern _mem ! Free memory list
.extern _cdbooted ! Whether we booted from CD
.extern _cddevice ! Whether we booted from CD
.text
! Set segment registers and stack pointer using the programs own header!
! The header is either 32 bytes (short form) or 48 bytes (long form). The
! bootblock will jump to address 0x10030 in both cases, calling one of the
! two jmpf instructions below.
! We assume boot is always linked with a short (32 byte) a.out header and has
! 16 bytes of its own prefix, so 48 bytes to skip. bootblock jumps into us
! at offset 0x30, cd boot code at 0x40
jmpf boot, LOADSEG+3 ! Set cs right (skipping long a.out header)
.space 11 ! jmpf + 11 = 16 bytes
jmpf boot, LOADSEG+2 ! Set cs right (skipping short a.out header)
! Set cs right
! (skip short a.out header plus 16 byte preefix)
jmpf boot, LOADSEG+3
.space 11
! entry point when booting from CD
jmpf cdboot, LOADSEG+3
.space 11
cdboot:
mov bx, #1
jmp commonboot
boot:
mov ax, #LOADSEG
mov bx, #0
commonboot:
mov ax, #LOADSEG+1
mov ds, ax ! ds = header
movb al, a_flags
@ -96,6 +107,7 @@ sepID:
mov _device, dx ! Boot device (probably 0x00 or 0x80)
mov _rem_part+0, si ! Remote partition table offset
pop _rem_part+2 ! and segment (saved es)
mov _cdbooted, bx ! Booted from CD? (bx set above)
! Remember the current video mode for restoration on exit.
movb ah, #0x0F ! Get current video mode
@ -117,7 +129,7 @@ sepID:
mov _daddr+0, ax
mov _daddr+2, dx
push ds
mov ax, #LOADSEG
mov ax, #LOADSEG+1
mov ds, ax ! Back to the header once more
mov ax, a_total+0
mov dx, a_total+2 ! dx:ax = data + bss + heap + stack
@ -416,6 +428,8 @@ _dev_open:
push es
push di ! Save registers used by BIOS calls
movb dl, _device ! The default device
cmpb dl, _cddevice
je cdopen
cmpb dl, #0x80 ! Floppy < 0x80, winchester >= 0x80
jae winchester
floppy:
@ -463,6 +477,10 @@ winchester:
jc geoerr ! No such drive?
andb cl, #0x3F ! cl = max sector number (1-origin)
incb dh ! dh = 1 + max head number (0-origin)
jmp geoboth
cdopen:
movb cl, #0x3F ! Think up geometry for CD's
movb dh, #0x2
geoboth:
movb sectors, cl ! Sectors per track
movb al, cl ! al = sectors per track
@ -515,20 +533,20 @@ _dev_boundary:
neg ax ! ax = (sector % sectors) == 0
ret
! int readsectors(u32_t bufaddr, u32_t sector, u8_t count)
! int biosreadsectors(u32_t bufaddr, u32_t sector, u8_t count)
! int writesectors(u32_t bufaddr, u32_t sector, u8_t count)
! Read/write several sectors from/to disk or floppy. The buffer must
! be between 64K boundaries! Count must fit in a byte. The external
! variables _device, sectors and secspcyl describe the disk and its
! geometry. Returns 0 for success, otherwise the BIOS error code.
!
.define _readsectors, _writesectors
.define _biosreadsectors, _writesectors
_writesectors:
push bp
mov bp, sp
movb 13(bp), #0x03 ! Code for a disk write
jmp rwsec
_readsectors:
_biosreadsectors:
push bp
mov bp, sp
movb 13(bp), #0x02 ! Code for a disk read
@ -555,6 +573,9 @@ more: mov ax, 8(bp)
mov dx, 10(bp) ! dx:ax = abs sector. Divide it by sectors/cyl
cmp dx, #[1024*255*63-255]>>16 ! Near 8G limit?
jae bigdisk
mov si, _device
cmp si, _cddevice ! Is it a CD?
je bigdisk ! CD's need extended read.
div secspcyl ! ax = cylinder, dx = sector within cylinder
xchg ax, dx ! ax = sector within cylinder, dx = cylinder
movb ch, dl ! ch = low 8 bits of cylinder
@ -1512,5 +1533,3 @@ p_mcs_desc:
.comm bus, 2 ! Saved return value of _get_bus
.comm unchar, 2 ! Char returned by ungetch(c)
.comm line, 2 ! Serial line I/O port to copy console I/O to.

View file

@ -496,7 +496,8 @@ void make_bootable(enum howto how, char *device, char *bootblock,
boothdr.a_magic[0]= !A_MAGIC0;
} else {
readblock(addr, buf, block_size);
memcpy(&boothdr, buf, sizeof(struct exec));
/* Must skip 16 bytes of 'boot' as that contains code. */
memcpy(&boothdr, buf + 16, sizeof(struct exec));
}
bootf= nil;
dummy.process= boothdr;

View file

@ -108,6 +108,7 @@ struct bc_validation {
#define INDICATE_BOOTABLE 0x88
#define BOOTMEDIA_UNSPECIFIED -1
#define BOOTMEDIA_NONE 0
#define BOOTMEDIA_120M 1
#define BOOTMEDIA_144M 2
@ -160,7 +161,8 @@ struct node {
};
int n_reserved_pathtableentries = 0, n_used_pathtableentries = 0;
int harddisk_emulation = 0;
int bootmedia = BOOTMEDIA_UNSPECIFIED;
unsigned long bootseg = 0;
int system_type = 0;
int get_system_type(int fd);
@ -736,15 +738,17 @@ writebootcatalog(int fd, int *currentsector, int imagesector, int imagesectors)
memset(&initial, 0, sizeof(initial));
initial.indicator = INDICATE_BOOTABLE;
if (harddisk_emulation)
initial.media = bootmedia;
initial.seg = (u_int16_t) (bootseg & 0xFFFF);
initial.sectors = 1;
if (bootmedia == BOOTMEDIA_HARDDISK)
{
initial.media = BOOTMEDIA_HARDDISK;
initial.type = system_type;
}
else
initial.media = BOOTMEDIA_144M;
/* initial.sectors = imagesectors; */
initial.sectors = 1;
if (bootmedia == BOOTMEDIA_NONE)
{
initial.sectors = imagesectors;
}
initial.startsector = imagesector;
written += Write(fd, &initial, sizeof(initial));
@ -857,10 +861,24 @@ main(int argc, char *argv[])
return 1;
}
while ((ch = getopt(argc, argv, "Rb:hl:")) != -1) {
while ((ch = getopt(argc, argv, "b:s:Rb:hl:nf")) != -1) {
switch(ch) {
case 's':
if(optarg[0] != '0' || optarg[1] != 'x') {
fprintf(stderr, "%s: -s<hex>\n",
argv[0]);
return 1;
}
bootseg = strtoul(optarg+2, NULL, 16);
break;
case 'h':
harddisk_emulation= 1;
bootmedia= BOOTMEDIA_HARDDISK;
break;
case 'n':
bootmedia= BOOTMEDIA_NONE;
break;
case 'f':
bootmedia= BOOTMEDIA_144M;
break;
case 'l':
label = optarg;
@ -881,8 +899,23 @@ main(int argc, char *argv[])
argc -= optind;
argv += optind;
/* Args check */
if(argc != 2) {
fprintf(stderr, "usage: %s [-l <label>] [-b <bootfloppyimage>] <dir> <isofile>\n",
fprintf(stderr, "usage: %s [-l <label>] [-b <bootimage> [-n] [-f] [-h] [-s <bootsegment>] ] <dir> <isofile>\n",
prog);
return 1;
}
if((bootimage && bootmedia == BOOTMEDIA_UNSPECIFIED) ||
(!bootimage && bootmedia != BOOTMEDIA_UNSPECIFIED)) {
fprintf(stderr, "%s: provide both boot image and boot type or neither.\n",
prog);
return 1;
}
if(!bootimage && bootseg) {
fprintf(stderr, "%s: boot seg provided but no boot image\n",
prog);
return 1;
}
@ -971,9 +1004,9 @@ main(int argc, char *argv[])
/* write the boot catalog */
fprintf(stderr, " * writing the boot catalog\n");
bootcatalogsector = currentsector;
if (harddisk_emulation)
if (bootmedia == BOOTMEDIA_HARDDISK)
system_type = get_system_type(bootfd);
writebootcatalog(fd, &currentsector, imagesector, imagesectors);
writebootcatalog(fd, &currentsector, 0, 0);
/* write boot image */
fprintf(stderr, " * writing the boot image\n");

View file

@ -9,10 +9,10 @@ mdec=/usr/mdec # bootstraps
# Check arguments.
case "$#:$1" in
1:bootable | 1:hdboot | [12]:fdboot | [123]:cdfdboot )
1:bootable | 1:hdboot | [12]:fdboot )
action=$1 dev=$2 size=$3
;;
*) echo "Usage: $0 [bootable | hdboot | fdboot [device]] | cdfdboot [device [size]]" >&2
*) echo "Usage: $0 [bootable | hdboot | fdboot [device]]" >&2
exit 1
esac
@ -161,53 +161,6 @@ fdboot)
edparams $dev 'main(){delay 2000;boot}; save' || exit
echo "Test kernel installed on $dev"
;;
cdfdboot)
# cdfdboot: Make a boot floppy image to go on a CD for booting from.
if [ -z "$dev" ]
then
ramdisk `expr 1440 \* 1024` 2>/dev/null
dev=/dev/ram
fi
if [ -z "$size" ]
then
size=1440
fi
umount $dev 2>/dev/null
if mkfs -B 1024 -b $size -i 512 $dev || exit 1
then :
else
echo "mkfs of $dev failed."
exit 1;
fi
# Install /dev, /boot
mount $dev /mnt || exit 1
mkdir /mnt/dev
mkdir /mnt/boot
mkdir /mnt/boot/image
( cd /mnt/dev && sh /usr/src/commands/scripts/MAKEDEV.sh std )
cp -p image /mnt/boot/image || exit 1
cp -p ../boot/boot /mnt/boot/boot || exit 1
umount $dev || exit 1
installboot -d $dev ../boot/bootblock boot/boot || exit 1
edparams $dev '
unset bootopts;
unset servers;
unset rootdev;
unset leader;
unset image;
disable=inet;
bootcd=1;
cdproberoot=1;
ata_id_timeout=2;
bootbig(1, Regular MINIX 3) { unset image; boot }
leader() { echo \n--- Welcome to MINIX 3. This is the boot monitor. ---\n\nChoose an option from the menu or press ESC if you need to do anything special.\nOtherwise I will boot with my defaults in 10 seconds.\n\n };
main(){trap 10000 boot; menu; };
save' || exit
# copy image
dd if=$dev of=cdfdimage bs=1024 count=$size
esac
sync
exit 0

View file

@ -107,7 +107,7 @@ usr=/dev/c0d7p0s2
RELEASEDIR=/usr/r
RELEASEPACKAGE=${RELEASEDIR}/usr/install/packages
RELEASEPACKAGESOURCES=${RELEASEDIR}/usr/install/package-sources
IMAGE=cdfdimage
IMAGE=../boot/boot
ROOTIMAGE=rootimage
CDFILES=/usr/tmp/cdreleasefiles
sh tell_config OS_RELEASE . OS_VERSION >/tmp/rel.$$
@ -210,7 +210,7 @@ then
fi
echo " * Cleanup old files"
rm -rf $RELEASEDIR $IMG $IMAGE $ROOTIMAGE $CDFILES image*
rm -rf $RELEASEDIR $IMG $ROOTIMAGE $CDFILES image*
mkdir -p $CDFILES || exit
mkdir -p $RELEASEDIR
mkfs -i 2000 -B $BS -b $ROOTBLOCKS $TMPDISK3 || exit
@ -288,18 +288,20 @@ then
#define _SVN_REVISION \"$REVTAG\"
#endif" >>$RELEASEDIR/usr/src/include/minix/sys_config.h
# output image name
if [ "$USB" -ne 0 ]; then
IMG=${IMG_BASE}_${REVTAG}.img
else
IMG=${IMG_BASE}_${REVTAG}.iso
fi
else
( cd .. && make depend && make clean )
srcdir=/usr/$SRC
( cd $srcdir && tar cf - . ) | ( cd $RELEASEDIR/usr && mkdir $SRC && cd $SRC && tar xf - )
REVTAG=copy
REVISION=unknown
fi
if [ "$USB" -ne 0 ]; then
IMG=${IMG_BASE}_${REVTAG}.img
else
IMG=${IMG_BASE}_${REVTAG}.iso
IMG=${IMG_BASE}_copy.iso
fi
echo " * Fixups for owners and modes of dirs and files"
@ -354,26 +356,37 @@ umount $TMPDISK1 || exit
umount $TMPDISK2 || exit
umount $TMPDISK3 || exit
# Boot monitor variables for boot CD
edparams $TMPDISK3 'unset bootopts;
unset servers;
unset rootdev;
unset leader;
unset image;
disable=inet;
bootcd=1;
cdproberoot=1;
ata_id_timeout=2;
bootbig(1, Regular MINIX 3) { unset image; boot }
leader() { echo \n--- Welcome to MINIX 3. This is the boot monitor. ---\n\nChoose an option from the menu or press ESC if you need to do anything special.\nOtherwise I will boot with my defaults in 10 seconds.\n\n }; main(){trap 10000 boot; menu; };
save'
(cd ../boot && make)
dd if=$TMPDISK3 of=$ROOTIMAGE bs=$BS count=$ROOTBLOCKS
sh mkboot cdfdboot $TMPDISK3 $ROOTKB
# image no longer fits on a floppy
#cp $IMAGE $CDFILES/bootflop.img
cp release/cd/* $CDFILES || true
echo "This is Minix version $version_pretty prepared `date`." >$CDFILES/VERSION.TXT
h_opt=
boottype=-n
bootimage=$IMAGE
if [ "$HDEMU" -ne 0 ]; then
make_hdimage
h_opt='-h'
boottype='-h'
bootimage=hdimage
fi
if [ "$USB" -ne 0 ]; then
mv $bootimage $IMG
else
writeisofs -l MINIX -b $bootimage $h_opt $CDFILES $IMG || exit 1
writeisofs -s0x1000 -l MINIX -b $bootimage $boottype $CDFILES $IMG || exit 1
if [ "$HDEMU" -eq 0 ]
then