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));
155 void write_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
160 dprintf("write_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
162 dio.startsector = sector;
164 dio.bufoffs = (uintptr_t) buf;
165 dio.bufseg = data_segment();
167 /* Try FAT32-aware system call first */
168 asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n"
171 : "a" (0x7305), "b" (&dio), "c" (-1), "d" (drive),
175 /* If not supported, try the legacy system call (int2526.S) */
176 if (errnum == 0x0001)
177 errnum = int26_write_sector(drive, &dio);
180 dprintf("rv = %04x\n", errnum);
181 die("sector write error");
185 void read_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
190 dprintf("read_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
192 dio.startsector = sector;
194 dio.bufoffs = (uintptr_t) buf;
195 dio.bufseg = data_segment();
197 /* Try FAT32-aware system call first */
198 asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n"
201 : "a" (0x7305), "b" (&dio), "c" (-1), "d" (drive),
204 /* If not supported, try the legacy system call (int2526.S) */
205 if (errnum == 0x0001)
206 errnum = int25_read_sector(drive, &dio);
209 dprintf("rv = %04x\n", errnum);
210 die("sector read error");
214 /* Both traditional DOS and FAT32 DOS return this structure, but
215 FAT32 return a lot more data, so make sure we have plenty of space */
216 struct deviceparams {
222 uint16_t bytespersec;
226 uint16_t rootdirents;
230 uint16_t secpertrack;
233 uint32_t hugesectors;
234 uint8_t lotsofpadding[224];
235 } __attribute__ ((packed));
237 uint32_t get_partition_offset(int drive)
241 struct deviceparams dp;
243 dp.specfunc = 1; /* Get current information */
246 asm volatile ("int $0x21 ; setc %0"
247 :"=abcdm" (err), "+a"(rv), "=m"(dp)
248 :"b" (drive), "c" (0x0860), "d" (&dp));
251 return dp.hiddensecs;
254 asm volatile ("int $0x21 ; setc %0"
255 : "=abcdm" (err), "+a" (rv), "=m" (dp)
256 : "b" (drive), "c" (0x4860), "d" (&dp));
259 return dp.hiddensecs;
261 die("could not find partition start offset");
268 uint16_t firstsector;
270 uint16_t bufferoffset;
272 } __attribute__ ((packed));
274 static struct rwblock mbr = {
278 .firstsector = 0, /* MS-DOS, unlike the BIOS, zero-base sectors */
284 void write_mbr(int drive, const void *buf)
289 dprintf("write_mbr(%d,%p)", drive, buf);
291 mbr.bufferoffset = (uintptr_t) buf;
292 mbr.bufferseg = data_segment();
295 asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err), "+a"(rv)
296 :"c"(0x0841), "d"(&mbr), "b"(drive), "m"(mbr));
298 dprintf(" rv(0841) = %04x", rv);
305 asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err), "+a"(rv)
306 :"c"(0x4841), "d"(&mbr), "b"(drive), "m"(mbr));
308 dprintf(" rv(4841) = %04x\n", rv);
310 die("mbr write error");
313 void read_mbr(int drive, const void *buf)
318 dprintf("read_mbr(%d,%p)", drive, buf);
320 mbr.bufferoffset = (uintptr_t) buf;
321 mbr.bufferseg = data_segment();
324 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
325 :"c"(0x0861), "d"(&mbr), "b"(drive), "m"(mbr));
327 dprintf(" rv(0861) = %04x", rv);
334 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
335 :"c"(0x4861), "d"(&mbr), "b"(drive), "m"(mbr));
337 dprintf(" rv(4841) = %04x\n", rv);
339 die("mbr read error");
341 dprintf("Bytes: %02x %02x %02x %02x %02x %02x %02x %02x\n",
342 ((const uint8_t *)buf)[0],
343 ((const uint8_t *)buf)[1],
344 ((const uint8_t *)buf)[2],
345 ((const uint8_t *)buf)[3],
346 ((const uint8_t *)buf)[4],
347 ((const uint8_t *)buf)[5],
348 ((const uint8_t *)buf)[6],
349 ((const uint8_t *)buf)[7]);
352 /* This call can legitimately fail, and we don't care, so ignore error return */
353 void set_attributes(const char *file, int attributes)
355 uint16_t rv = 0x4301;
357 dprintf("set_attributes(\"%s\", 0x%02x)\n", file, attributes);
359 asm volatile ("int $0x21":"+a" (rv)
360 :"c"(attributes), "d"(file));
364 * Version of the read_device function suitable for libfat
366 int libfat_xpread(intptr_t pp, void *buf, size_t secsize,
367 libfat_sector_t sector)
369 read_device(pp, buf, 1, sector);
373 static inline void get_dos_version(void)
377 asm("int $0x21 ; xchgb %%ah,%%al"
383 dprintf("DOS version %d.%d\n", (dos_version >> 8), dos_version & 0xff);
386 /* The locking interface relies on static variables. A massive hack :( */
387 static uint8_t lock_level, lock_drive;
389 static inline void set_lock_device(uint8_t device)
395 static int do_lock(uint8_t level)
397 uint16_t level_arg = lock_drive + (level << 8);
401 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
402 uint16_t lock_call = (dos_version >= 0x070a) ? 0x484A : 0x084A;
404 uint16_t lock_call = 0x084A; /* MSDN says this is OK for all filesystems */
407 dprintf("Trying lock %04x... ", level_arg);
408 asm volatile ("int $0x21 ; setc %0"
409 : "=bcdm" (err), "=a" (rv)
410 : "a" (0x440d), "b" (level_arg),
411 "c" (lock_call), "d" (0x0001));
412 dprintf("%s %04x\n", err ? "err" : "ok", rv);
417 void lock_device(int level)
419 static int hard_lock = 0;
422 if (dos_version < 0x0700)
423 return; /* Win9x/NT only */
426 /* Assume hierarchial "soft" locking supported */
428 while (lock_level < level) {
429 int new_level = lock_level + 1;
430 err = do_lock(new_level);
433 /* Try hard locking next */
439 lock_level = new_level;
446 /* Hard locking, only level 4 supported */
447 /* This is needed for Win9x in DOS mode */
452 /* Assume locking is not needed */
463 die("could not lock device");
466 void unlock_device(int level)
470 uint16_t unlock_call;
472 if (dos_version < 0x0700)
473 return; /* Win9x/NT only */
476 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
477 unlock_call = (dos_version >= 0x070a) ? 0x486A : 0x086A;
479 unlock_call = 0x086A; /* MSDN says this is OK for all filesystems */
482 if (lock_level == 4 && level > 0)
483 return; /* Only drop the hard lock at the end */
485 while (lock_level > level) {
486 uint8_t new_level = (lock_level == 4) ? 0 : lock_level - 1;
487 uint16_t level_arg = (new_level << 8) + lock_drive;
489 dprintf("Trying unlock %04x... ", new_level);
490 asm volatile ("int $0x21 ; setc %0"
491 : "=bcdm" (err), "+a" (rv)
492 : "b" (level_arg), "c" (unlock_call));
493 dprintf("%s %04x\n", err ? "err" : "ok", rv);
494 lock_level = new_level;
499 * This function does any desired MBR manipulation; called with the device lock held.
502 uint8_t active; /* Active flag */
503 uint8_t bhead; /* Begin head */
504 uint8_t bsector; /* Begin sector */
505 uint8_t bcylinder; /* Begin cylinder */
506 uint8_t filesystem; /* Filesystem value */
507 uint8_t ehead; /* End head */
508 uint8_t esector; /* End sector */
509 uint8_t ecylinder; /* End cylinder */
510 uint32_t startlba; /* Start sector LBA */
511 uint32_t sectors; /* Length in sectors */
512 } __attribute__ ((packed));
514 static void adjust_mbr(int device, int writembr, int set_active)
516 static unsigned char sectbuf[512];
519 if (!writembr && !set_active)
520 return; /* Nothing to do */
522 read_mbr(device, sectbuf);
525 memcpy(sectbuf, syslinux_mbr, syslinux_mbr_len);
526 *(uint16_t *) (sectbuf + 510) = 0xaa55;
530 uint32_t offset = get_partition_offset(device);
531 struct mbr_entry *me = (struct mbr_entry *)(sectbuf + 446);
534 dprintf("Searching for partition offset: %08x\n", offset);
536 for (i = 0; i < 4; i++) {
537 if (me->startlba == offset) {
547 die("partition not found (-a is not implemented for logical partitions)");
548 } else if (found > 1) {
549 die("multiple aliased partitions found");
553 write_mbr(device, sectbuf);
556 int main(int argc, char *argv[])
558 static unsigned char sectbuf[512];
560 static char ldlinux_name[] = "@:\\ldlinux.sys";
562 int force = 0; /* -f (force) option */
563 struct libfat_filesystem *fs;
564 libfat_sector_t s, *secp, sectors[65]; /* 65 is maximum possible */
565 int32_t ldlinux_cluster;
567 const char *device = NULL, *bootsecfile = NULL;
570 int writembr = 0; /* -m (write MBR) option */
571 int set_active = 0; /* -a (set partition active) option */
572 const char *subdir = NULL;
576 dprintf("argv = %p\n", argv);
577 for (i = 0; i <= argc; i++)
578 dprintf("argv[%d] = %p = \"%s\"\n", i, argv[i], argv[i]);
580 (void)argc; /* Unused */
584 for (argp = argv + 1; *argp; argp++) {
592 case 's': /* Use "safe, slow and stupid" code */
595 case 'r': /* RAID mode */
598 case 'f': /* Force install */
601 case 'm': /* Write MBR */
604 case 'a': /* Set partition active */
630 * Figure out which drive we're talking to
632 dev_fd = (device[0] & ~0x20) - 0x40;
633 if (dev_fd < 1 || dev_fd > 26 || device[1] != ':' || device[2])
636 set_lock_device(dev_fd);
638 lock_device(2); /* Make sure we can lock the device */
639 read_device(dev_fd, sectbuf, 1, 0);
643 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
645 if ((errmsg = syslinux_check_bootsect(sectbuf))) {
652 ldlinux_name[0] = dev_fd | 0x40;
654 set_attributes(ldlinux_name, 0x00);
655 fd = creat(ldlinux_name, 0);
656 write_file(fd, syslinux_ldlinux, syslinux_ldlinux_len);
658 set_attributes(ldlinux_name, 0x07); /* SYSTEM HIDDEN READONLY */
661 * Now, use libfat to create a block map. This probably
662 * should be changed to use ioctl(...,FIBMAP,...) since
663 * this is supposed to be a simple, privileged version
667 fs = libfat_open(libfat_xpread, dev_fd);
668 ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL);
671 s = libfat_clustertosector(fs, ldlinux_cluster);
672 while (s && nsectors < 65) {
675 s = libfat_nextsector(fs, s);
680 * If requested, move ldlinux.sys
683 char new_ldlinux_name[160];
684 char *cp = new_ldlinux_name + 3;
688 new_ldlinux_name[0] = dev_fd | 0x40;
689 new_ldlinux_name[1] = ':';
690 new_ldlinux_name[2] = '\\';
692 for (sd = subdir; *sd; sd++) {
695 if (c == '/' || c == '\\') {
707 /* Skip if subdirectory == root */
708 if (cp > new_ldlinux_name + 3) {
712 memcpy(cp, "ldlinux.sys", 12);
714 set_attributes(ldlinux_name, 0);
715 if (rename(ldlinux_name, new_ldlinux_name))
716 set_attributes(ldlinux_name, 0x07);
718 set_attributes(new_ldlinux_name, 0x07);
723 * Patch ldlinux.sys and the boot sector
725 syslinux_patch(sectors, nsectors, stupid, raid_mode);
728 * Write the now-patched first sector of ldlinux.sys
730 /* lock_device(3); -- doesn't seem to be needed */
731 write_device(dev_fd, syslinux_ldlinux, 1, sectors[0]);
734 * Muck with the MBR, if desired, while we hold the lock
736 adjust_mbr(dev_fd, writembr, set_active);
739 * To finish up, write the boot sector
742 /* Read the superblock again since it might have changed while mounted */
743 read_device(dev_fd, sectbuf, 1, 0);
745 /* Copy the syslinux code into the boot sector */
746 syslinux_make_bootsect(sectbuf);
748 /* Write new boot sector */
751 fd = creat(bootsecfile, 0x20); /* ARCHIVE */
752 write_file(fd, sectbuf, 512);
755 write_device(dev_fd, sectbuf, 1, 0);