1 #ident "$Id: syslinux.c,v 1.13 2004/12/28 22:51:44 hpa Exp $"
2 /* ----------------------------------------------------------------------- *
4 * Copyright 1998-2004 H. Peter Anvin - All Rights Reserved
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9 * Boston MA 02111-1307, USA; either version 2 of the License, or
10 * (at your option) any later version; incorporated herein by reference.
12 * ----------------------------------------------------------------------- */
15 * syslinux.c - Linux installer program for SYSLINUX
29 const char *program = "syslinux"; /* Name of program */
33 # define dprintf printf
35 # define dprintf(...) ((void)0)
38 void __attribute__((noreturn)) usage(void)
40 puts("Usage: syslinux [-sfma] <drive>: [bootsecfile]\n");
44 void unlock_device(int);
46 void __attribute__((noreturn)) die(const char *msg)
56 * read/write wrapper functions
58 int creat(const char *filename, int mode)
63 dprintf("creat(\"%s\", 0x%x)\n", filename, mode);
66 asm volatile("int $0x21 ; setc %0"
67 : "=abcdm" (err), "+a" (rv)
68 : "c" (mode), "d" (filename));
70 dprintf("rv = %d\n", rv);
71 die("cannot open ldlinux.sys");
81 dprintf("close(%d)\n", fd);
83 asm volatile("int $0x21"
87 /* The only error MS-DOS returns for close is EBADF,
88 and we really don't care... */
91 ssize_t write_file(int fd, const void *buf, size_t count)
97 dprintf("write_file(%d,%p,%u)\n", fd, buf, count);
101 asm volatile("int $0x21 ; setc %0"
102 : "=abcdm" (err), "+a" (rv)
103 : "b" (fd), "c" (count), "d" (buf));
104 if ( err || rv == 0 )
105 die("file write error");
115 uint32_t startsector;
117 uint16_t bufoffs, bufseg;
118 } __attribute__((packed));
120 void write_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
125 dprintf("write_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
127 dio.startsector = sector;
129 dio.bufoffs = (uintptr_t)buf;
130 asm("movw %%ds,%0" : "=m" (dio.bufseg));
132 asm volatile("int $0x26 ; setc %0 ; popfw"
134 : "a" (drive-1), "b" (&dio), "c" (-1), "d" (buf));
137 die("sector write error");
140 void read_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
145 dprintf("read_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
147 dio.startsector = sector;
149 dio.bufoffs = (uintptr_t)buf;
150 asm("movw %%ds,%0" : "=m" (dio.bufseg));
152 asm volatile("int $0x25 ; setc %0 ; popfw"
154 : "a" (drive-1), "b" (&dio), "c" (-1), "d" (buf));
157 die("sector read error");
160 /* Both traditional DOS and FAT32 DOS return this structure, but
161 FAT32 return a lot more data, so make sure we have plenty of space */
162 struct deviceparams {
168 uint16_t bytespersec;
172 uint16_t rootdirents;
176 uint16_t secpertrack;
179 uint32_t hugesectors;
180 uint8_t lotsofpadding[224];
181 } __attribute__((packed));
183 uint32_t get_partition_offset(int drive)
187 struct deviceparams dp;
189 dp.specfunc = 1; /* Get current information */
192 asm volatile("int $0x21 ; setc %0"
193 : "=abcdm" (err), "+a" (rv)
194 : "b" (drive), "c" (0x0860), "d" (&dp));
197 return dp.hiddensecs;
200 asm volatile("int $0x21 ; setc %0"
201 : "=abcdm" (err), "+a" (rv)
202 : "b" (drive), "c" (0x4860), "d" (&dp));
205 return dp.hiddensecs;
207 die("could not find partition start offset");
214 uint16_t firstsector;
216 uint16_t bufferoffset;
218 } __attribute__((packed));
220 static struct rwblock mbr = {
224 .firstsector = 0, /* MS-DOS, unlike the BIOS, zero-base sectors */
230 void write_mbr(int drive, const void *buf)
235 dprintf("write_mbr(%d,%p)\n", drive, buf);
237 mbr.bufferoffset = (uintptr_t)buf;
238 asm("movw %%ds,%0" : "=m" (mbr.bufferseg));
241 asm volatile("int $0x21 ; setc %0"
242 : "=abcdm" (err), "+a" (rv)
243 : "c" (0x0841), "d" (&mbr), "b" (drive));
249 asm volatile("int $0x21 ; setc %0"
250 : "=abcdm" (err), "+a" (rv)
251 : "c" (0x4841), "d" (&mbr), "b" (drive));
254 die("mbr write error");
257 void read_mbr(int drive, const void *buf)
262 dprintf("read_mbr(%d,%p)\n", drive, buf);
264 mbr.bufferoffset = (uintptr_t)buf;
265 asm("movw %%ds,%0" : "=m" (mbr.bufferseg));
268 asm volatile("int $0x21 ; setc %0"
269 : "=abcdm" (err), "+a" (rv)
270 : "c" (0x0861), "d" (&mbr), "b" (drive));
276 asm volatile("int $0x21 ; setc %0"
277 : "=abcdm" (err), "+a" (rv)
278 : "c" (0x4861), "d" (&mbr), "b" (drive));
281 die("mbr read error");
284 /* This call can legitimately fail, and we don't care, so ignore error return */
285 void set_attributes(const char *file, int attributes)
287 uint16_t rv = 0x4301;
289 dprintf("set_attributes(\"%s\", 0x%02x)\n", file, attributes);
291 asm volatile("int $0x21"
293 : "c" (attributes), "d" (file));
297 * Version of the read_device function suitable for libfat
299 int libfat_xpread(intptr_t pp, void *buf, size_t secsize, libfat_sector_t sector)
301 read_device(pp, buf, 1, sector);
305 static inline void get_dos_version(void)
307 uint16_t ver = 0x3001;
308 asm("int $0x21 ; xchgb %%ah,%%al" : "+a" (ver) : : "ebx", "ecx");
310 dprintf("DOS version %d.%d\n", (dos_version >> 8), dos_version & 0xff);
313 /* The locking interface relies on static variables. A massive hack :( */
314 static uint16_t lock_level;
316 static inline void set_lock_device(uint8_t device)
321 void lock_device(int level)
327 if ( dos_version < 0x0700 )
328 return; /* Win9x/NT only */
331 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
332 lock_call = (dos_version >= 0x0710) ? 0x484A : 0x084A;
334 lock_call = 0x084A; /* MSDN says this is OK for all filesystems */
337 while ( (lock_level >> 8) < level ) {
338 uint16_t new_level = lock_level + 0x0100;
339 dprintf("Trying lock %04x...\n", new_level);
341 asm volatile("int $0x21 ; setc %0"
342 : "=abcdm" (err), "+a" (rv)
343 : "b" (new_level), "c" (lock_call), "d"(0x0001));
345 /* rv == 0x0001 means this call is not supported, if so we
346 assume locking isn't needed (e.g. Win9x in DOS-only mode) */
350 die("could not lock device");
353 lock_level = new_level;
358 void unlock_device(int level)
362 uint16_t unlock_call;
364 if ( dos_version < 0x0700 )
365 return; /* Win9x/NT only */
368 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
369 unlock_call = (dos_version >= 0x0710) ? 0x486A : 0x086A;
371 unlock_call = 0x086A; /* MSDN says this is OK for all filesystems */
374 while ( (lock_level >> 8) > level ) {
375 uint16_t new_level = lock_level - 0x0100;
377 asm volatile("int $0x21 ; setc %0"
378 : "=abcdm" (err), "+a" (rv)
379 : "b" (new_level), "c" (unlock_call));
380 lock_level = new_level;
386 * This function does any desired MBR manipulation; called with the device lock held.
389 uint8_t active; /* Active flag */
390 uint8_t bhead; /* Begin head */
391 uint8_t bsector; /* Begin sector */
392 uint8_t bcylinder; /* Begin cylinder */
393 uint8_t filesystem; /* Filesystem value */
394 uint8_t ehead; /* End head */
395 uint8_t esector; /* End sector */
396 uint8_t ecylinder; /* End cylinder */
397 uint32_t startlba; /* Start sector LBA */
398 uint32_t sectors; /* Length in sectors */
399 } __attribute__((packed));
401 static void adjust_mbr(int device, int writembr, int set_active)
403 static unsigned char sectbuf[512];
406 if ( !writembr && !set_active )
407 return; /* Nothing to do */
409 read_mbr(device, sectbuf);
412 memcpy(sectbuf, syslinux_mbr, syslinux_mbr_len);
413 *(uint16_t *)(sectbuf+510) = 0xaa55;
417 uint32_t offset = get_partition_offset(device);
418 struct mbr_entry *me = (struct mbr_entry *)(sectbuf+446);
421 for ( i = 0 ; i < 4 ; i++ ) {
422 if ( me->startlba == offset ) {
432 die("partition not found");
433 } else if ( found > 1 ) {
434 die("multiple aliased partitions found");
438 write_mbr(device, sectbuf);
441 int main(int argc, char *argv[])
443 static unsigned char sectbuf[512];
445 static char ldlinux_name[] = "@:\\LDLINUX.SYS";
447 int force = 0; /* -f (force) option */
448 struct libfat_filesystem *fs;
449 libfat_sector_t s, *secp, sectors[65]; /* 65 is maximum possible */
450 int32_t ldlinux_cluster;
452 const char *device = NULL, *bootsecfile = NULL;
455 int writembr = 0; /* -m (write MBR) option */
456 int set_active = 0; /* -a (set partition active) option */
458 dprintf("argv = %p\n", argv);
459 for ( i = 0 ; i <= argc ; i++ )
460 dprintf("argv[%d] = %p = \"%s\"\n", i, argv[i], argv[i]);
462 (void)argc; /* Unused */
466 for ( argp = argv+1 ; *argp ; argp++ ) {
467 if ( **argp == '-' ) {
474 case 's': /* Use "safe, slow and stupid" code */
475 syslinux_make_stupid();
477 case 'f': /* Force install */
480 case 'm': /* Write MBR */
483 case 'a': /* Set partition active */
505 * Figure out which drive we're talking to
507 dev_fd = (device[0] & ~0x20) - 0x40;
508 if ( dev_fd < 1 || dev_fd > 26 || device[1] != ':' || device[2] )
511 set_lock_device(dev_fd);
513 lock_device(2); /* Make sure we can lock the device */
514 read_device(dev_fd, sectbuf, 1, 0);
518 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
520 if( (errmsg = syslinux_check_bootsect(sectbuf)) ) {
527 ldlinux_name[0] = dev_fd | 0x40;
529 set_attributes(ldlinux_name, 0);
530 fd = creat(ldlinux_name, 0x07); /* SYSTEM HIDDEN READONLY */
531 write_file(fd, syslinux_ldlinux, syslinux_ldlinux_len);
535 * Now, use libfat to create a block map. This probably
536 * should be changed to use ioctl(...,FIBMAP,...) since
537 * this is supposed to be a simple, privileged version
541 fs = libfat_open(libfat_xpread, dev_fd);
542 ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL);
545 s = libfat_clustertosector(fs, ldlinux_cluster);
546 while ( s && nsectors < 65 ) {
549 s = libfat_nextsector(fs, s);
554 * Patch ldlinux.sys and the boot sector
556 syslinux_patch(sectors, nsectors);
559 * Write the now-patched first sector of ldlinux.sys
562 write_device(dev_fd, syslinux_ldlinux, 1, sectors[0]);
565 * Muck with the MBR, if desired, while we hold the lock
567 adjust_mbr(dev_fd, writembr, set_active);
570 * To finish up, write the boot sector
573 /* Read the superblock again since it might have changed while mounted */
574 read_device(dev_fd, sectbuf, 1, 0);
576 /* Copy the syslinux code into the boot sector */
577 syslinux_make_bootsect(sectbuf);
579 /* Write new boot sector */
582 fd = creat(bootsecfile, 0x20); /* ARCHIVE */
583 write_file(fd, sectbuf, 512);
586 write_device(dev_fd, sectbuf, 1, 0);