Skip to content

Commit

Permalink
[boot] Stamp start sector offset into boot sector for MBR boots
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaerr committed Nov 9, 2020
1 parent 184c396 commit 8d81130
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 35 deletions.
24 changes: 5 additions & 19 deletions bootblocks/boot_sect.S
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,6 @@ floppy_table:

entry1:
#endif

// instructions through _next0 not needed for floppy, only MBR boot
cld
cmp $0x4c65,%ax // coming from ELKS MBR?
jnz _next0
mov 8(%si),%ax // get MBR start sector from partion table at DS:SI
mov 10(%si),%si
push %cs
pop %es
mov $BOOTADDR+sect_offset,%di
stosw
xchg %ax,%si
stosw
_next0:

// Get the memory size
// TODO: optional BIOS INT 12h

Expand Down Expand Up @@ -559,16 +544,17 @@ msg_error:
msg_reboot:
.asciz "Press key\r\n"

.global end_of_code
end_of_code:
//------------------------------------------------------------------------------

#ifndef BOOT_FAT
.org 0x1F3 // Fixed address of sect_offset for MINIX boot sector
.global sect_offset
sect_offset:
.long 0
#endif

.global last_code
last_code:
//------------------------------------------------------------------------------

// ELKS disk parameter structure
// For future expansion, fields should be added at the _front_ of structure

Expand Down
1 change: 0 additions & 1 deletion bootblocks/mbr.S
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ sector_loaded:

mov drive_num,%dh // Restore DH drive number for VBR
mov %si,%bp // LILO says some BBs use bp rather than si
mov $0x4c65,%ax // 'eL' ELKS magic number

ljmp $0,$BOOTADDR // Jump to VBR boot sector

Expand Down
49 changes: 47 additions & 2 deletions elks/tools/setboot/setboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* 7 Feb 2020 Greg Haerr <[email protected]>
*
* Usage: setboot <image> [-F] [-{B,P}<sectors>,<heads>[,<tracks>]] [<input_boot_sector>]
* Usage: setboot <image> [-F] [-S{m,f}] [-{B,P}<sectors>,<heads>[,<tracks>]] [<input_boot_sector>]
*
* setboot writes image after optionally reading an input boot sector and
* optionally modifying boot sector disk parameters passed as parameters.
Expand Down Expand Up @@ -33,11 +33,18 @@
#define ELKS_BPB_SecPerTrk 0x1F9 /* offset of sectors per track (byte)*/
#define ELKS_BPB_NumHeads 0x1FA /* offset of number of heads (byte)*/

/* MINIX-only offsets*/
#define MINIX_SectOffset 0x1F3 /* offset of partition start sector (long)*/

/* FAT BPB start and end offsets*/
#define FATBPB_START 11 /* start at bytes per sector*/
#define FATBPB_END 61 /* through end of file system type*/

/* FAT-only offsets*/
#define FAT_BPB_SectOffset 0x1C /* offset of partition start sector (long) */

static unsigned int SecPerTrk, NumHeads, NumTracks;
static unsigned long StartSector;

struct partition
{
Expand Down Expand Up @@ -75,6 +82,7 @@ static void writePartition(unsigned char *buf)
p->cyl = 0;
p->start_sect = 1; /* zero-relative start sector here*/
#endif
StartSector = p->start_sect;
p->sys_ind = 0x80; /* ELKS, Old Minix*/
p->end_head = NumHeads;
p->end_sector = SecPerTrk | ((NumTracks >> 2) & 0xc0);
Expand Down Expand Up @@ -141,12 +149,14 @@ int main(int argc,char **argv)
int opt_new_bootsect = 0, opt_updatebpb = 0;
int opt_skipfatbpb = 0;
int opt_writepartitiontable = 0;
int opt_update_start_sector_minix = 0;
int opt_update_start_sector_fat = 0;
char *outfile, *infile = NULL;
unsigned char blk[SECT_SIZE*2];
unsigned char inblk[SECT_SIZE];

if (argc != 3 && argc != 4 && argc != 5)
fatalmsg("Usage: %s <image> [-F] [-{B,P}<sectors>,<heads>[,<tracks>]] [<input_boot_image>]\n", argv[0]);
fatalmsg("Usage: %s <image> [-F] [-S{m,f}] [-{B,P}<sectors>,<heads>[,<tracks>]] [<input_boot_image>]\n", argv[0]);

outfile = *++argv; argc--;

Expand Down Expand Up @@ -181,6 +191,13 @@ int main(int argc,char **argv)
argv++;
argc--;
}
else if (argv[1][1] == 'S') { /* update start sector in ELKS boot sector*/
if (argv[1][2] == 'm') opt_update_start_sector_minix = 1;
if (argv[1][2] == 'f') opt_update_start_sector_fat = 1;
printf("Updating start sector in ELKS boot sector\n");
argv++;
argc--;
}
}
if (argc == 2) { /* new boot sector specified*/
infile = *++argv;
Expand Down Expand Up @@ -241,6 +258,34 @@ int main(int argc,char **argv)
fprintf(stderr, "Warning: '%s' may not be valid bootable sector\n", infile);

if (fwrite(blk,1,count,ofp) != count) die("fwrite(%s)", infile);

if (opt_update_start_sector_minix || opt_update_start_sector_fat) {
if (fseek(ofp, StartSector * SECT_SIZE, SEEK_SET) != 0)
die("fseek(%s)", outfile);

count = fread(blk,1,SECT_SIZE,ofp);
if (count != SECT_SIZE)
die("fread(%s)", outfile);

if (opt_update_start_sector_minix) {
blk[MINIX_SectOffset] = (unsigned char)StartSector;
blk[MINIX_SectOffset+1] = (unsigned char)(StartSector >> 8);
blk[MINIX_SectOffset+2] = (unsigned char)(StartSector >> 16);
blk[MINIX_SectOffset+3] = (unsigned char)(StartSector >> 24);
}
if (opt_update_start_sector_fat) {
blk[FAT_BPB_SectOffset] = (unsigned char)StartSector;
blk[FAT_BPB_SectOffset+1] = (unsigned char)(StartSector >> 8);
blk[FAT_BPB_SectOffset+2] = (unsigned char)(StartSector >> 16);
blk[FAT_BPB_SectOffset+3] = (unsigned char)(StartSector >> 24);
}

if (fseek(ofp, StartSector * SECT_SIZE, SEEK_SET) != 0)
die("fseek(%s)", outfile);
if (fwrite(blk,1,SECT_SIZE,ofp) != SECT_SIZE)
die("fwrite(%s)", outfile);
}

fclose(ofp);
fclose(ifp);
} else { /* perform BPB update only on existing boot block*/
Expand Down
21 changes: 10 additions & 11 deletions elkscmd/sys_utils/makeboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
#define ELKS_BPB_SecPerTrk 0x1F9 /* offset of sectors per track (byte)*/
#define ELKS_BPB_NumHeads 0x1FA /* offset of number of heads (byte)*/

/* MINIX-only offsets, FIXME - SectOffset not yet standard*/
#define MINIX_SectOffset 0x1B5 /* offset of partition start sector FIXME */
/* MINIX-only offsets*/
#define MINIX_SectOffset 0x1F3 /* offset of partition start sector (long)*/

/* FAT BPB start and end offsets*/
#define FATBPB_START 11 /* start at bytes per sector*/
Expand Down Expand Up @@ -332,17 +332,13 @@ int main(int ac, char **av)
return -1;
}
close(fd);
//sync();
//return 0;

if (mkdir(MOUNTDIR, 0777) < 0) {
fprintf(stderr, "Can't create temp mount point %s\n", MOUNTDIR);
return -1;
}
if (mkdir(MOUNTDIR, 0777) < 0)
fprintf(stderr, "Can't create temp mount point %s, may already exist\n", MOUNTDIR);

if (mount(targetdevice, MOUNTDIR, fstype, 0) < 0) {
fprintf(stderr, "Can't mount %s on %s\n", targetdevice, MOUNTDIR);
rmdir(MOUNTDIR);
return -1;
goto errout2;
}
if (!copyfile(SYSFILE1, MOUNTDIR SYSFILE1, 1)) {
fprintf(stderr, "Error copying %s\n", SYSFILE1);
Expand All @@ -360,11 +356,14 @@ int main(int ac, char **av)
if (umount(targetdevice) < 0)
fprintf(stderr, "Unmount error\n");
rmdir(MOUNTDIR);
sync();

return fstype; /* return filesystem type (1=MINIX, 2=FAT */

errout:
umount(targetdevice);
errout2:
rmdir(MOUNTDIR);
return 0;
sync();
return -1;
}
4 changes: 2 additions & 2 deletions image/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ hd32-fat:
# MBR images
hd32mbr-minix:
dd if=/dev/zero bs=512 count=63 | cat - hd32-minix.bin > hd32mbr-minix.bin
setboot hd32mbr-minix.bin -P63,16,63 $(HD_MBR_BOOT)
setboot hd32mbr-minix.bin -P63,16,63 -Sm $(HD_MBR_BOOT)

hd32mbr-fat:
dd if=/dev/zero bs=512 count=63 | cat - hd32-fat.bin > hd32mbr-fat.bin
setboot hd32mbr-fat.bin -P63,16,63 $(HD_MBR_BOOT)
setboot hd32mbr-fat.bin -P63,16,63 -Sf $(HD_MBR_BOOT)

# Clean target

Expand Down

0 comments on commit 8d81130

Please sign in to comment.