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":"=bcdm" (err), "+a"(rv)
73 :"c"(mode), "d"(filename));
75 dprintf("rv = %d\n", rv);
76 die("cannot open ldlinux.sys");
86 dprintf("close(%d)\n", fd);
88 asm volatile ("int $0x21":"+a" (rv)
91 /* The only error MS-DOS returns for close is EBADF,
92 and we really don't care... */
95 int rename(const char *oldname, const char *newname)
97 uint16_t rv = 0x5600; /* Also support 43FFh? */
100 dprintf("rename(\"%s\", \"%s\")\n", oldname, newname);
102 asm volatile ("int $0x21 ; setc %0":"=bcdm" (err), "+a"(rv)
103 :"d"(oldname), "D"(newname));
106 dprintf("rv = %d\n", rv);
107 warning("cannot move ldlinux.sys");
114 ssize_t write_file(int fd, const void *buf, size_t count)
120 dprintf("write_file(%d,%p,%u)\n", fd, buf, count);
124 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
125 :"b"(fd), "c"(count), "d"(buf));
127 die("file write error");
136 static inline __attribute__ ((const))
137 uint16_t data_segment(void)
141 asm("movw %%ds,%0":"=rm"(ds));
146 uint32_t startsector;
148 uint16_t bufoffs, bufseg;
149 } __attribute__ ((packed));
151 void write_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
156 dprintf("write_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
158 dio.startsector = sector;
160 dio.bufoffs = (uintptr_t) buf;
161 dio.bufseg = data_segment();
163 asm volatile ("int $0x26 ; setc %0 ; popfw":"=abcdm" (err)
164 :"a"(drive - 1), "b"(&dio), "c"(-1), "d"(buf), "m"(dio));
167 die("sector write error");
170 void read_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
175 dprintf("read_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
177 dio.startsector = sector;
179 dio.bufoffs = (uintptr_t) buf;
180 dio.bufseg = data_segment();
182 asm volatile ("int $0x25 ; setc %0 ; popfw":"=abcdm" (err)
183 :"a"(drive - 1), "b"(&dio), "c"(-1), "d"(buf), "m"(dio));
186 die("sector read error");
189 /* Both traditional DOS and FAT32 DOS return this structure, but
190 FAT32 return a lot more data, so make sure we have plenty of space */
191 struct deviceparams {
197 uint16_t bytespersec;
201 uint16_t rootdirents;
205 uint16_t secpertrack;
208 uint32_t hugesectors;
209 uint8_t lotsofpadding[224];
210 } __attribute__ ((packed));
212 uint32_t get_partition_offset(int drive)
216 struct deviceparams dp;
218 dp.specfunc = 1; /* Get current information */
221 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv), "=m"(dp)
222 :"b"(drive), "c"(0x0860), "d"(&dp));
225 return dp.hiddensecs;
228 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv), "=m"(dp)
229 :"b"(drive), "c"(0x4860), "d"(&dp));
232 return dp.hiddensecs;
234 die("could not find partition start offset");
241 uint16_t firstsector;
243 uint16_t bufferoffset;
245 } __attribute__ ((packed));
247 static struct rwblock mbr = {
251 .firstsector = 0, /* MS-DOS, unlike the BIOS, zero-base sectors */
257 void write_mbr(int drive, const void *buf)
262 dprintf("write_mbr(%d,%p)\n", drive, buf);
264 mbr.bufferoffset = (uintptr_t) buf;
265 mbr.bufferseg = data_segment();
268 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
269 :"c"(0x0841), "d"(&mbr), "b"(drive), "m"(mbr));
275 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
276 :"c"(0x4841), "d"(&mbr), "b"(drive), "m"(mbr));
279 die("mbr write error");
282 void read_mbr(int drive, const void *buf)
287 dprintf("read_mbr(%d,%p)\n", drive, buf);
289 mbr.bufferoffset = (uintptr_t) buf;
290 mbr.bufferseg = data_segment();
293 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
294 :"c"(0x0861), "d"(&mbr), "b"(drive), "m"(mbr));
300 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
301 :"c"(0x4861), "d"(&mbr), "b"(drive), "m"(mbr));
304 die("mbr read error");
307 /* This call can legitimately fail, and we don't care, so ignore error return */
308 void set_attributes(const char *file, int attributes)
310 uint16_t rv = 0x4301;
312 dprintf("set_attributes(\"%s\", 0x%02x)\n", file, attributes);
314 asm volatile ("int $0x21":"+a" (rv)
315 :"c"(attributes), "d"(file));
319 * Version of the read_device function suitable for libfat
321 int libfat_xpread(intptr_t pp, void *buf, size_t secsize,
322 libfat_sector_t sector)
324 read_device(pp, buf, 1, sector);
328 static inline void get_dos_version(void)
330 uint16_t ver = 0x3001;
331 asm("int $0x21 ; xchgb %%ah,%%al": "+a"(ver): :"ebx", "ecx");
333 dprintf("DOS version %d.%d\n", (dos_version >> 8), dos_version & 0xff);
336 /* The locking interface relies on static variables. A massive hack :( */
337 static uint16_t lock_level;
339 static inline void set_lock_device(uint8_t device)
344 void lock_device(int level)
350 if (dos_version < 0x0700)
351 return; /* Win9x/NT only */
354 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
355 lock_call = (dos_version >= 0x0710) ? 0x484A : 0x084A;
357 lock_call = 0x084A; /* MSDN says this is OK for all filesystems */
360 while ((lock_level >> 8) < level) {
361 uint16_t new_level = lock_level + 0x0100;
362 dprintf("Trying lock %04x...\n", new_level);
364 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
365 :"b"(new_level), "c"(lock_call), "d"(0x0001));
367 /* rv == 0x0001 means this call is not supported, if so we
368 assume locking isn't needed (e.g. Win9x in DOS-only mode) */
372 die("could not lock device");
375 lock_level = new_level;
380 void unlock_device(int level)
384 uint16_t unlock_call;
386 if (dos_version < 0x0700)
387 return; /* Win9x/NT only */
390 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
391 unlock_call = (dos_version >= 0x0710) ? 0x486A : 0x086A;
393 unlock_call = 0x086A; /* MSDN says this is OK for all filesystems */
396 while ((lock_level >> 8) > level) {
397 uint16_t new_level = lock_level - 0x0100;
399 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
400 :"b"(new_level), "c"(unlock_call));
401 lock_level = new_level;
406 * This function does any desired MBR manipulation; called with the device lock held.
409 uint8_t active; /* Active flag */
410 uint8_t bhead; /* Begin head */
411 uint8_t bsector; /* Begin sector */
412 uint8_t bcylinder; /* Begin cylinder */
413 uint8_t filesystem; /* Filesystem value */
414 uint8_t ehead; /* End head */
415 uint8_t esector; /* End sector */
416 uint8_t ecylinder; /* End cylinder */
417 uint32_t startlba; /* Start sector LBA */
418 uint32_t sectors; /* Length in sectors */
419 } __attribute__ ((packed));
421 static void adjust_mbr(int device, int writembr, int set_active)
423 static unsigned char sectbuf[512];
426 if (!writembr && !set_active)
427 return; /* Nothing to do */
429 read_mbr(device, sectbuf);
432 memcpy(sectbuf, syslinux_mbr, syslinux_mbr_len);
433 *(uint16_t *) (sectbuf + 510) = 0xaa55;
437 uint32_t offset = get_partition_offset(device);
438 struct mbr_entry *me = (struct mbr_entry *)(sectbuf + 446);
441 for (i = 0; i < 4; i++) {
442 if (me->startlba == offset) {
452 die("partition not found");
453 } else if (found > 1) {
454 die("multiple aliased partitions found");
458 write_mbr(device, sectbuf);
461 int main(int argc, char *argv[])
463 static unsigned char sectbuf[512];
465 static char ldlinux_name[] = "@:\\ldlinux.sys";
467 int force = 0; /* -f (force) option */
468 struct libfat_filesystem *fs;
469 libfat_sector_t s, *secp, sectors[65]; /* 65 is maximum possible */
470 int32_t ldlinux_cluster;
472 const char *device = NULL, *bootsecfile = NULL;
475 int writembr = 0; /* -m (write MBR) option */
476 int set_active = 0; /* -a (set partition active) option */
477 const char *subdir = NULL;
481 dprintf("argv = %p\n", argv);
482 for (i = 0; i <= argc; i++)
483 dprintf("argv[%d] = %p = \"%s\"\n", i, argv[i], argv[i]);
485 (void)argc; /* Unused */
489 for (argp = argv + 1; *argp; argp++) {
497 case 's': /* Use "safe, slow and stupid" code */
500 case 'r': /* RAID mode */
503 case 'f': /* Force install */
506 case 'm': /* Write MBR */
509 case 'a': /* Set partition active */
535 * Figure out which drive we're talking to
537 dev_fd = (device[0] & ~0x20) - 0x40;
538 if (dev_fd < 1 || dev_fd > 26 || device[1] != ':' || device[2])
541 set_lock_device(dev_fd);
543 lock_device(2); /* Make sure we can lock the device */
544 read_device(dev_fd, sectbuf, 1, 0);
548 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
550 if ((errmsg = syslinux_check_bootsect(sectbuf))) {
557 ldlinux_name[0] = dev_fd | 0x40;
559 set_attributes(ldlinux_name, 0);
560 fd = creat(ldlinux_name, 0x07); /* SYSTEM HIDDEN READONLY */
561 write_file(fd, syslinux_ldlinux, syslinux_ldlinux_len);
565 * Now, use libfat to create a block map. This probably
566 * should be changed to use ioctl(...,FIBMAP,...) since
567 * this is supposed to be a simple, privileged version
571 fs = libfat_open(libfat_xpread, dev_fd);
572 ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL);
575 s = libfat_clustertosector(fs, ldlinux_cluster);
576 while (s && nsectors < 65) {
579 s = libfat_nextsector(fs, s);
584 * If requested, move ldlinux.sys
587 char new_ldlinux_name[160];
588 char *cp = new_ldlinux_name + 3;
592 new_ldlinux_name[0] = dev_fd | 0x40;
593 new_ldlinux_name[1] = ':';
594 new_ldlinux_name[2] = '\\';
596 for (sd = subdir; *sd; sd++) {
599 if (c == '/' || c == '\\') {
611 /* Skip if subdirectory == root */
612 if (cp > new_ldlinux_name + 3) {
616 memcpy(cp, "ldlinux.sys", 12);
618 set_attributes(ldlinux_name, 0);
619 if (rename(ldlinux_name, new_ldlinux_name))
620 set_attributes(ldlinux_name, 0x07);
622 set_attributes(new_ldlinux_name, 0x07);
627 * Patch ldlinux.sys and the boot sector
629 syslinux_patch(sectors, nsectors, stupid, raid_mode);
632 * Write the now-patched first sector of ldlinux.sys
635 write_device(dev_fd, syslinux_ldlinux, 1, sectors[0]);
638 * Muck with the MBR, if desired, while we hold the lock
640 adjust_mbr(dev_fd, writembr, set_active);
643 * To finish up, write the boot sector
646 /* Read the superblock again since it might have changed while mounted */
647 read_device(dev_fd, sectbuf, 1, 0);
649 /* Copy the syslinux code into the boot sector */
650 syslinux_make_bootsect(sectbuf);
652 /* Write new boot sector */
655 fd = creat(bootsecfile, 0x20); /* ARCHIVE */
656 write_file(fd, sectbuf, 512);
659 write_device(dev_fd, sectbuf, 1, 0);