1 /* ----------------------------------------------------------------------- *
3 * Copyright 1998-2008 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
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
30 const char *program = "syslinux"; /* Name of program */
34 # define dprintf printf
39 asm volatile("int $0x16" : "=a" (ax) : "a" (0));
42 # define dprintf(...) ((void)0)
43 # define pause() ((void)0)
46 void __attribute__ ((noreturn)) usage(void)
48 puts("Usage: syslinux [-sfmar][-d directory] <drive>: [bootsecfile]\n");
52 void unlock_device(int);
54 void __attribute__ ((noreturn)) die(const char *msg)
63 void warning(const char *msg)
65 puts("syslinux: warning: ");
71 * read/write wrapper functions
73 int creat(const char *filename, int mode)
78 dprintf("creat(\"%s\", 0x%x)\n", filename, mode);
81 asm volatile ("int $0x21 ; setc %0"
82 : "=bcdm" (err), "+a" (rv)
83 : "c" (mode), "d" (filename));
85 dprintf("rv = %d\n", rv);
86 die("cannot open ldlinux.sys");
96 dprintf("close(%d)\n", fd);
98 asm volatile ("int $0x21":"+a" (rv)
101 /* The only error MS-DOS returns for close is EBADF,
102 and we really don't care... */
105 int rename(const char *oldname, const char *newname)
107 uint16_t rv = 0x5600; /* Also support 43FFh? */
110 dprintf("rename(\"%s\", \"%s\")\n", oldname, newname);
112 asm volatile ("int $0x21 ; setc %0":"=bcdm" (err), "+a"(rv)
113 :"d"(oldname), "D"(newname));
116 dprintf("rv = %d\n", rv);
117 warning("cannot move ldlinux.sys");
124 ssize_t write_file(int fd, const void *buf, size_t count)
130 dprintf("write_file(%d,%p,%u)\n", fd, buf, count);
134 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
135 :"b"(fd), "c"(count), "d"(buf));
137 die("file write error");
146 static inline __attribute__ ((const))
147 uint16_t data_segment(void)
151 asm("movw %%ds,%0" : "=rm"(ds));
156 uint32_t startsector;
158 uint16_t bufoffs, bufseg;
159 } __attribute__ ((packed));
161 void write_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
167 dprintf("write_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
169 dio.startsector = sector;
171 dio.bufoffs = (uintptr_t) buf;
172 dio.bufseg = data_segment();
174 if (dos_version >= 0x0710) {
175 asm volatile("int $0x21 ; setc %0"
176 : "=bcdm" (err), "=a" (errnum)
177 : "a" (0x7305), "b" (&dio), "c" (-1), "d" (drive),
181 asm volatile("int $0x26 ; setc %0 ; popfw"
182 : "=bcdm" (err), "=a" (errnum)
183 : "a" (drive-1), "b" (&dio), "c" (-1), "d" (buf),
189 dprintf("rv = %04x\n", errnum);
190 die("sector write error");
194 void read_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
200 dprintf("read_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
202 dio.startsector = sector;
204 dio.bufoffs = (uintptr_t) buf;
205 dio.bufseg = data_segment();
207 if (dos_version >= 0x0710) {
208 asm volatile("int $0x21 ; setc %0"
209 : "=bcdm" (err), "=a" (errnum)
210 : "a" (0x7305), "b" (&dio), "c" (-1), "d" (drive),
213 asm volatile("int $0x25 ; setc %0 ; popfw"
214 : "=bcdm" (err), "=a" (errnum)
215 : "a" (drive-1), "b" (&dio), "c" (-1), "d" (buf),
221 dprintf("rv = %04x\n", errnum);
222 die("sector read error");
226 /* Both traditional DOS and FAT32 DOS return this structure, but
227 FAT32 return a lot more data, so make sure we have plenty of space */
228 struct deviceparams {
234 uint16_t bytespersec;
238 uint16_t rootdirents;
242 uint16_t secpertrack;
245 uint32_t hugesectors;
246 uint8_t lotsofpadding[224];
247 } __attribute__ ((packed));
249 uint32_t get_partition_offset(int drive)
253 struct deviceparams dp;
255 dp.specfunc = 1; /* Get current information */
258 asm volatile ("int $0x21 ; setc %0"
259 :"=abcdm" (err), "+a"(rv), "=m"(dp)
260 :"b" (drive), "c" (0x0860), "d" (&dp));
263 return dp.hiddensecs;
266 asm volatile ("int $0x21 ; setc %0"
267 : "=abcdm" (err), "+a" (rv), "=m" (dp)
268 : "b" (drive), "c" (0x4860), "d" (&dp));
271 return dp.hiddensecs;
273 die("could not find partition start offset");
280 uint16_t firstsector;
282 uint16_t bufferoffset;
284 } __attribute__ ((packed));
286 static struct rwblock mbr = {
290 .firstsector = 0, /* MS-DOS, unlike the BIOS, zero-base sectors */
296 void write_mbr(int drive, const void *buf)
301 dprintf("write_mbr(%d,%p)", drive, buf);
303 mbr.bufferoffset = (uintptr_t) buf;
304 mbr.bufferseg = data_segment();
307 asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err), "+a"(rv)
308 :"c"(0x0841), "d"(&mbr), "b"(drive), "m"(mbr));
310 dprintf(" rv(0841) = %04x", rv);
317 asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err), "+a"(rv)
318 :"c"(0x4841), "d"(&mbr), "b"(drive), "m"(mbr));
320 dprintf(" rv(4841) = %04x\n", rv);
322 die("mbr write error");
325 void read_mbr(int drive, const void *buf)
330 dprintf("read_mbr(%d,%p)", drive, buf);
332 mbr.bufferoffset = (uintptr_t) buf;
333 mbr.bufferseg = data_segment();
336 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
337 :"c"(0x0861), "d"(&mbr), "b"(drive), "m"(mbr));
339 dprintf(" rv(0861) = %04x", rv);
346 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
347 :"c"(0x4861), "d"(&mbr), "b"(drive), "m"(mbr));
349 dprintf(" rv(4841) = %04x\n", rv);
351 die("mbr read error");
353 dprintf("Bytes: %02x %02x %02x %02x %02x %02x %02x %02x\n",
354 ((const uint8_t *)buf)[0],
355 ((const uint8_t *)buf)[1],
356 ((const uint8_t *)buf)[2],
357 ((const uint8_t *)buf)[3],
358 ((const uint8_t *)buf)[4],
359 ((const uint8_t *)buf)[5],
360 ((const uint8_t *)buf)[6],
361 ((const uint8_t *)buf)[7]);
364 /* This call can legitimately fail, and we don't care, so ignore error return */
365 void set_attributes(const char *file, int attributes)
367 uint16_t rv = 0x4301;
369 dprintf("set_attributes(\"%s\", 0x%02x)\n", file, attributes);
371 asm volatile ("int $0x21":"+a" (rv)
372 :"c"(attributes), "d"(file));
376 * Version of the read_device function suitable for libfat
378 int libfat_xpread(intptr_t pp, void *buf, size_t secsize,
379 libfat_sector_t sector)
381 read_device(pp, buf, 1, sector);
385 static inline void get_dos_version(void)
389 asm("int $0x21 ; xchgb %%ah,%%al"
395 dprintf("DOS version %d.%d\n", (dos_version >> 8), dos_version & 0xff);
398 /* The locking interface relies on static variables. A massive hack :( */
399 static uint8_t lock_level, lock_drive;
401 static inline void set_lock_device(uint8_t device)
407 static int do_lock(uint8_t level)
409 uint16_t level_arg = lock_drive + (level << 8);
413 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
414 uint16_t lock_call = (dos_version >= 0x0710) ? 0x484A : 0x084A;
416 uint16_t lock_call = 0x084A; /* MSDN says this is OK for all filesystems */
419 dprintf("Trying lock %04x... ", level_arg);
420 asm volatile ("int $0x21 ; setc %0"
421 : "=bcdm" (err), "=a" (rv)
422 : "a" (0x440d), "b" (level_arg),
423 "c" (lock_call), "d" (0x0001));
424 dprintf("%s %04x\n", err ? "err" : "ok", rv);
429 void lock_device(int level)
431 static int hard_lock = 0;
434 if (dos_version < 0x0700)
435 return; /* Win9x/NT only */
438 /* Assume hierarchial "soft" locking supported */
440 while (lock_level < level) {
441 int new_level = lock_level + 1;
442 err = do_lock(new_level);
445 /* Try hard locking next */
451 lock_level = new_level;
458 /* Hard locking, only level 4 supported */
459 /* This is needed for Win9x in DOS mode */
464 /* Assume locking is not needed */
475 die("could not lock device");
478 void unlock_device(int level)
482 uint16_t unlock_call;
484 if (dos_version < 0x0700)
485 return; /* Win9x/NT only */
488 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
489 unlock_call = (dos_version >= 0x0710) ? 0x486A : 0x086A;
491 unlock_call = 0x086A; /* MSDN says this is OK for all filesystems */
494 if (lock_level == 4 && level > 0)
495 return; /* Only drop the hard lock at the end */
497 while (lock_level > level) {
498 uint8_t new_level = (lock_level == 4) ? 0 : lock_level - 1;
499 uint16_t level_arg = (new_level << 8) + lock_drive;
501 dprintf("Trying unlock %04x... ", new_level);
502 asm volatile ("int $0x21 ; setc %0"
503 : "=bcdm" (err), "+a" (rv)
504 : "b" (level_arg), "c" (unlock_call));
505 dprintf("%s %04x\n", err ? "err" : "ok", rv);
506 lock_level = new_level;
511 * This function does any desired MBR manipulation; called with the device lock held.
514 uint8_t active; /* Active flag */
515 uint8_t bhead; /* Begin head */
516 uint8_t bsector; /* Begin sector */
517 uint8_t bcylinder; /* Begin cylinder */
518 uint8_t filesystem; /* Filesystem value */
519 uint8_t ehead; /* End head */
520 uint8_t esector; /* End sector */
521 uint8_t ecylinder; /* End cylinder */
522 uint32_t startlba; /* Start sector LBA */
523 uint32_t sectors; /* Length in sectors */
524 } __attribute__ ((packed));
526 static void adjust_mbr(int device, int writembr, int set_active)
528 static unsigned char sectbuf[512];
531 if (!writembr && !set_active)
532 return; /* Nothing to do */
534 read_mbr(device, sectbuf);
537 memcpy(sectbuf, syslinux_mbr, syslinux_mbr_len);
538 *(uint16_t *) (sectbuf + 510) = 0xaa55;
542 uint32_t offset = get_partition_offset(device);
543 struct mbr_entry *me = (struct mbr_entry *)(sectbuf + 446);
546 dprintf("Searching for partition offset: %08x\n", offset);
548 for (i = 0; i < 4; i++) {
549 if (me->startlba == offset) {
559 die("partition not found (-a is not implemented for logical partitions)");
560 } else if (found > 1) {
561 die("multiple aliased partitions found");
565 write_mbr(device, sectbuf);
568 int main(int argc, char *argv[])
570 static unsigned char sectbuf[512];
572 static char ldlinux_name[] = "@:\\ldlinux.sys";
574 int force = 0; /* -f (force) option */
575 struct libfat_filesystem *fs;
576 libfat_sector_t s, *secp, sectors[65]; /* 65 is maximum possible */
577 int32_t ldlinux_cluster;
579 const char *device = NULL, *bootsecfile = NULL;
582 int writembr = 0; /* -m (write MBR) option */
583 int set_active = 0; /* -a (set partition active) option */
584 const char *subdir = NULL;
588 dprintf("argv = %p\n", argv);
589 for (i = 0; i <= argc; i++)
590 dprintf("argv[%d] = %p = \"%s\"\n", i, argv[i], argv[i]);
592 (void)argc; /* Unused */
596 for (argp = argv + 1; *argp; argp++) {
604 case 's': /* Use "safe, slow and stupid" code */
607 case 'r': /* RAID mode */
610 case 'f': /* Force install */
613 case 'm': /* Write MBR */
616 case 'a': /* Set partition active */
642 * Figure out which drive we're talking to
644 dev_fd = (device[0] & ~0x20) - 0x40;
645 if (dev_fd < 1 || dev_fd > 26 || device[1] != ':' || device[2])
648 set_lock_device(dev_fd);
650 lock_device(2); /* Make sure we can lock the device */
651 read_device(dev_fd, sectbuf, 1, 0);
655 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
657 if ((errmsg = syslinux_check_bootsect(sectbuf))) {
664 ldlinux_name[0] = dev_fd | 0x40;
666 set_attributes(ldlinux_name, 0x00);
667 fd = creat(ldlinux_name, 0);
668 write_file(fd, syslinux_ldlinux, syslinux_ldlinux_len);
670 set_attributes(ldlinux_name, 0x07); /* SYSTEM HIDDEN READONLY */
673 * Now, use libfat to create a block map. This probably
674 * should be changed to use ioctl(...,FIBMAP,...) since
675 * this is supposed to be a simple, privileged version
679 fs = libfat_open(libfat_xpread, dev_fd);
680 ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL);
683 s = libfat_clustertosector(fs, ldlinux_cluster);
684 while (s && nsectors < 65) {
687 s = libfat_nextsector(fs, s);
692 * If requested, move ldlinux.sys
695 char new_ldlinux_name[160];
696 char *cp = new_ldlinux_name + 3;
700 new_ldlinux_name[0] = dev_fd | 0x40;
701 new_ldlinux_name[1] = ':';
702 new_ldlinux_name[2] = '\\';
704 for (sd = subdir; *sd; sd++) {
707 if (c == '/' || c == '\\') {
719 /* Skip if subdirectory == root */
720 if (cp > new_ldlinux_name + 3) {
724 memcpy(cp, "ldlinux.sys", 12);
726 set_attributes(ldlinux_name, 0);
727 if (rename(ldlinux_name, new_ldlinux_name))
728 set_attributes(ldlinux_name, 0x07);
730 set_attributes(new_ldlinux_name, 0x07);
735 * Patch ldlinux.sys and the boot sector
737 syslinux_patch(sectors, nsectors, stupid, raid_mode);
740 * Write the now-patched first sector of ldlinux.sys
742 /* lock_device(3); -- doesn't seem to be needed */
743 write_device(dev_fd, syslinux_ldlinux, 1, sectors[0]);
746 * Muck with the MBR, if desired, while we hold the lock
748 adjust_mbr(dev_fd, writembr, set_active);
751 * To finish up, write the boot sector
754 /* Read the superblock again since it might have changed while mounted */
755 read_device(dev_fd, sectbuf, 1, 0);
757 /* Copy the syslinux code into the boot sector */
758 syslinux_make_bootsect(sectbuf);
760 /* Write new boot sector */
763 fd = creat(bootsecfile, 0x20); /* ARCHIVE */
764 write_file(fd, sectbuf, 512);
767 write_device(dev_fd, sectbuf, 1, 0);