1 /* ----------------------------------------------------------------------- *
3 * Copyright 1998-2008 H. Peter Anvin - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
14 * syslinux.c - Linux installer program for SYSLINUX
28 const char *program = "syslinux"; /* Name of program */
32 # define dprintf printf
34 # define dprintf(...) ((void)0)
37 void __attribute__((noreturn)) usage(void)
39 puts("Usage: syslinux [-sfmar][-d directory] <drive>: [bootsecfile]\n");
43 void unlock_device(int);
45 void __attribute__((noreturn)) die(const char *msg)
54 void warning(const char *msg)
56 puts("syslinux: warning: ");
62 * read/write wrapper functions
64 int creat(const char *filename, int mode)
69 dprintf("creat(\"%s\", 0x%x)\n", filename, mode);
72 asm volatile("int $0x21 ; setc %0"
73 : "=bcdm" (err), "+a" (rv)
74 : "c" (mode), "d" (filename));
76 dprintf("rv = %d\n", rv);
77 die("cannot open ldlinux.sys");
87 dprintf("close(%d)\n", fd);
89 asm volatile("int $0x21"
93 /* The only error MS-DOS returns for close is EBADF,
94 and we really don't care... */
97 int rename(const char *oldname, const char *newname)
99 uint16_t rv = 0x5600; /* Also support 43FFh? */
102 dprintf("rename(\"%s\", \"%s\")\n", oldname, newname);
104 asm volatile("int $0x21 ; setc %0"
105 : "=bcdm" (err), "+a" (rv)
106 : "d" (oldname), "D" (newname));
109 dprintf("rv = %d\n", rv);
110 warning("cannot move ldlinux.sys");
117 extern const char __payload_sseg[];
118 static uint16_t ldlinux_seg;
120 ssize_t write_ldlinux(int fd)
126 while (offset < syslinux_ldlinux_len) {
127 uint32_t chunk = syslinux_ldlinux_len - offset;
130 asm volatile("pushw %%ds ; "
135 : "=bcdm" (err), "=a" (rv)
136 : "a" (0x4000), "b" (fd), "c" (chunk), "d" (offset & 15),
137 "SD" ((uint16_t)(ldlinux_seg + (offset >> 4))));
138 if ( err || rv == 0 )
139 die("file write error");
146 ssize_t write_file(int fd, const void *buf, size_t count)
152 dprintf("write_file(%d,%p,%u)\n", fd, buf, count);
155 asm volatile("int $0x21 ; setc %0"
156 : "=bcdm" (err), "=a" (rv)
157 : "a" (0x4000), "b" (fd), "c" (count), "d" (buf));
158 if ( err || rv == 0 )
159 die("file write error");
168 static inline __attribute__((const)) uint16_t data_segment(void)
172 asm("movw %%ds,%0" : "=rm" (ds));
177 uint32_t startsector;
179 uint16_t bufoffs, bufseg;
180 } __attribute__((packed));
182 void write_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
187 dprintf("write_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
189 dio.startsector = sector;
191 dio.bufoffs = (uintptr_t)buf;
192 dio.bufseg = data_segment();
194 asm volatile("int $0x26 ; setc %0 ; popfw"
196 : "a" (drive-1), "b" (&dio), "c" (-1), "d" (buf), "m" (dio));
199 die("sector write error");
202 void read_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
207 dprintf("read_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
209 dio.startsector = sector;
211 dio.bufoffs = (uintptr_t)buf;
212 dio.bufseg = data_segment();
214 asm volatile("int $0x25 ; setc %0 ; popfw"
216 : "a" (drive-1), "b" (&dio), "c" (-1), "d" (buf), "m" (dio));
219 die("sector read error");
222 /* Both traditional DOS and FAT32 DOS return this structure, but
223 FAT32 return a lot more data, so make sure we have plenty of space */
224 struct deviceparams {
230 uint16_t bytespersec;
234 uint16_t rootdirents;
238 uint16_t secpertrack;
241 uint32_t hugesectors;
242 uint8_t lotsofpadding[224];
243 } __attribute__((packed));
245 uint32_t get_partition_offset(int drive)
249 struct deviceparams dp;
251 dp.specfunc = 1; /* Get current information */
254 asm volatile("int $0x21 ; setc %0"
255 : "=abcdm" (err), "+a" (rv), "=m" (dp)
256 : "b" (drive), "c" (0x0860), "d" (&dp));
259 return dp.hiddensecs;
262 asm volatile("int $0x21 ; setc %0"
263 : "=abcdm" (err), "+a" (rv), "=m" (dp)
264 : "b" (drive), "c" (0x4860), "d" (&dp));
267 return dp.hiddensecs;
269 die("could not find partition start offset");
276 uint16_t firstsector;
278 uint16_t bufferoffset;
280 } __attribute__((packed));
282 static struct rwblock mbr = {
286 .firstsector = 0, /* MS-DOS, unlike the BIOS, zero-base sectors */
292 void write_mbr(int drive, const void *buf)
297 dprintf("write_mbr(%d,%p)\n", drive, buf);
299 mbr.bufferoffset = (uintptr_t)buf;
300 mbr.bufferseg = data_segment();
303 asm volatile("int $0x21 ; setc %0"
304 : "=abcdm" (err), "+a" (rv)
305 : "c" (0x0841), "d" (&mbr), "b" (drive), "m" (mbr));
311 asm volatile("int $0x21 ; setc %0"
312 : "=abcdm" (err), "+a" (rv)
313 : "c" (0x4841), "d" (&mbr), "b" (drive), "m" (mbr));
316 die("mbr write error");
319 void read_mbr(int drive, const void *buf)
324 dprintf("read_mbr(%d,%p)\n", drive, buf);
326 mbr.bufferoffset = (uintptr_t)buf;
327 mbr.bufferseg = data_segment();
330 asm volatile("int $0x21 ; setc %0"
331 : "=abcdm" (err), "+a" (rv)
332 : "c" (0x0861), "d" (&mbr), "b" (drive), "m" (mbr));
338 asm volatile("int $0x21 ; setc %0"
339 : "=abcdm" (err), "+a" (rv)
340 : "c" (0x4861), "d" (&mbr), "b" (drive), "m" (mbr));
343 die("mbr read error");
346 /* This call can legitimately fail, and we don't care, so ignore error return */
347 void set_attributes(const char *file, int attributes)
349 uint16_t rv = 0x4301;
351 dprintf("set_attributes(\"%s\", 0x%02x)\n", file, attributes);
353 asm volatile("int $0x21"
355 : "c" (attributes), "d" (file));
359 * Version of the read_device function suitable for libfat
361 int libfat_xpread(intptr_t pp, void *buf, size_t secsize, libfat_sector_t sector)
363 read_device(pp, buf, 1, sector);
367 static inline void get_dos_version(void)
369 uint16_t ver = 0x3001;
370 asm("int $0x21 ; xchgb %%ah,%%al" : "+a" (ver) : : "ebx", "ecx");
372 dprintf("DOS version %d.%d\n", (dos_version >> 8), dos_version & 0xff);
375 /* The locking interface relies on static variables. A massive hack :( */
376 static uint16_t lock_level;
378 static inline void set_lock_device(uint8_t device)
383 void lock_device(int level)
389 if ( dos_version < 0x0700 )
390 return; /* Win9x/NT only */
393 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
394 lock_call = (dos_version >= 0x0710) ? 0x484A : 0x084A;
396 lock_call = 0x084A; /* MSDN says this is OK for all filesystems */
399 while ( (lock_level >> 8) < level ) {
400 uint16_t new_level = lock_level + 0x0100;
401 dprintf("Trying lock %04x...\n", new_level);
403 asm volatile("int $0x21 ; setc %0"
404 : "=abcdm" (err), "+a" (rv)
405 : "b" (new_level), "c" (lock_call), "d"(0x0001));
407 /* rv == 0x0001 means this call is not supported, if so we
408 assume locking isn't needed (e.g. Win9x in DOS-only mode) */
412 die("could not lock device");
415 lock_level = new_level;
420 void unlock_device(int level)
424 uint16_t unlock_call;
426 if ( dos_version < 0x0700 )
427 return; /* Win9x/NT only */
430 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
431 unlock_call = (dos_version >= 0x0710) ? 0x486A : 0x086A;
433 unlock_call = 0x086A; /* MSDN says this is OK for all filesystems */
436 while ( (lock_level >> 8) > level ) {
437 uint16_t new_level = lock_level - 0x0100;
439 asm volatile("int $0x21 ; setc %0"
440 : "=abcdm" (err), "+a" (rv)
441 : "b" (new_level), "c" (unlock_call));
442 lock_level = new_level;
448 * This function does any desired MBR manipulation; called with the device lock held.
451 uint8_t active; /* Active flag */
452 uint8_t bhead; /* Begin head */
453 uint8_t bsector; /* Begin sector */
454 uint8_t bcylinder; /* Begin cylinder */
455 uint8_t filesystem; /* Filesystem value */
456 uint8_t ehead; /* End head */
457 uint8_t esector; /* End sector */
458 uint8_t ecylinder; /* End cylinder */
459 uint32_t startlba; /* Start sector LBA */
460 uint32_t sectors; /* Length in sectors */
461 } __attribute__((packed));
463 static void adjust_mbr(int device, int writembr, int set_active)
465 static unsigned char sectbuf[SECTOR_SIZE];
468 if ( !writembr && !set_active )
469 return; /* Nothing to do */
471 read_mbr(device, sectbuf);
474 memcpy(sectbuf, syslinux_mbr, syslinux_mbr_len);
475 *(uint16_t *)(sectbuf+510) = 0xaa55;
479 uint32_t offset = get_partition_offset(device);
480 struct mbr_entry *me = (struct mbr_entry *)(sectbuf+446);
483 for ( i = 0 ; i < 4 ; i++ ) {
484 if ( me->startlba == offset ) {
494 die("partition not found");
495 } else if ( found > 1 ) {
496 die("multiple aliased partitions found");
500 write_mbr(device, sectbuf);
503 int main(int argc, char *argv[])
505 static unsigned char sectbuf[SECTOR_SIZE];
507 static char ldlinux_name[] = "@:\\ldlinux.sys";
509 int force = 0; /* -f (force) option */
510 struct libfat_filesystem *fs;
511 libfat_sector_t s, *secp;
512 libfat_sector_t *sectors;
514 int32_t ldlinux_cluster;
516 const char *device = NULL, *bootsecfile = NULL;
519 int writembr = 0; /* -m (write MBR) option */
520 int set_active = 0; /* -a (set partition active) option */
521 const char *subdir = NULL;
526 ldlinux_seg = (size_t)__payload_sseg + data_segment();
528 dprintf("argv = %p\n", argv);
529 for ( i = 0 ; i <= argc ; i++ )
530 dprintf("argv[%d] = %p = \"%s\"\n", i, argv[i], argv[i]);
532 (void)argc; /* Unused */
536 for ( argp = argv+1 ; *argp ; argp++ ) {
537 if ( **argp == '-' ) {
544 case 's': /* Use "safe, slow and stupid" code */
547 case 'r': /* RAID mode */
550 case 'f': /* Force install */
553 case 'm': /* Write MBR */
556 case 'a': /* Set partition active */
582 * Figure out which drive we're talking to
584 dev_fd = (device[0] & ~0x20) - 0x40;
585 if ( dev_fd < 1 || dev_fd > 26 || device[1] != ':' || device[2] )
588 set_lock_device(dev_fd);
590 lock_device(2); /* Make sure we can lock the device */
591 read_device(dev_fd, sectbuf, 1, 0);
595 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
597 if( (errmsg = syslinux_check_bootsect(sectbuf)) ) {
604 ldlinux_name[0] = dev_fd | 0x40;
606 set_attributes(ldlinux_name, 0);
607 fd = creat(ldlinux_name, 0x07); /* SYSTEM HIDDEN READONLY */
612 * Now, use libfat to create a block map. This probably
613 * should be changed to use ioctl(...,FIBMAP,...) since
614 * this is supposed to be a simple, privileged version
617 ldlinux_sectors = (syslinux_ldlinux_len+SECTOR_SIZE-1) >> SECTOR_SHIFT;
618 sectors = calloc(ldlinux_sectors, sizeof *sectors);
620 fs = libfat_open(libfat_xpread, dev_fd);
621 ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL);
624 s = libfat_clustertosector(fs, ldlinux_cluster);
625 while ( s && nsectors < ldlinux_sectors ) {
628 s = libfat_nextsector(fs, s);
633 * If requested, move ldlinux.sys
636 char new_ldlinux_name[160];
637 char *cp = new_ldlinux_name+3;
641 new_ldlinux_name[0] = dev_fd | 0x40;
642 new_ldlinux_name[1] = ':';
643 new_ldlinux_name[2] = '\\';
645 for (sd = subdir; *sd; sd++) {
648 if (c == '/' || c == '\\') {
660 /* Skip if subdirectory == root */
661 if (cp > new_ldlinux_name+3) {
665 memcpy(cp, "ldlinux.sys", 12);
667 set_attributes(ldlinux_name, 0);
668 if (rename(ldlinux_name, new_ldlinux_name))
669 set_attributes(ldlinux_name, 0x07);
671 set_attributes(new_ldlinux_name, 0x07);
676 * Patch ldlinux.sys and the boot sector
678 i = syslinux_patch(sectors, nsectors, stupid, raid_mode);
679 patch_sectors = (i+SECTOR_SIZE-1) >> SECTOR_SHIFT;
682 * Overwrite the now-patched ldlinux.sys
685 for (i = 0; i < patch_sectors; i++) {
688 di = (size_t)sectbuf;
689 cx = SECTOR_SIZE >> 2;
690 asm volatile("movw %3,%%fs ; fs ; rep ; movsl"
691 : "+S" (si), "+D" (di), "+c" (cx)
692 : "abd" ((uint16_t)(ldlinux_seg + (i << (SECTOR_SHIFT-4)))));
693 write_device(dev_fd, sectbuf, 1, sectors[i]);
697 * Muck with the MBR, if desired, while we hold the lock
699 adjust_mbr(dev_fd, writembr, set_active);
702 * To finish up, write the boot sector
705 /* Read the superblock again since it might have changed while mounted */
706 read_device(dev_fd, sectbuf, 1, 0);
708 /* Copy the syslinux code into the boot sector */
709 syslinux_make_bootsect(sectbuf);
711 /* Write new boot sector */
714 fd = creat(bootsecfile, 0x20); /* ARCHIVE */
715 write_file(fd, sectbuf, SECTOR_SIZE);
718 write_device(dev_fd, sectbuf, 1, 0);