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 extern const char __payload_sseg
[];
125 uint16_t ldlinux_seg
;
127 ssize_t
write_ldlinux(int fd
)
133 while (offset
< syslinux_ldlinux_len
) {
134 uint32_t chunk
= syslinux_ldlinux_len
- offset
;
137 asm volatile ("pushw %%ds ; "
140 "popw %%ds ; " "setc %0":"=bcdm" (err
), "=a"(rv
)
141 :"a"(0x4000), "b"(fd
), "c"(chunk
), "d"(offset
& 15),
142 "SD"((uint16_t) (ldlinux_seg
+ (offset
>> 4))));
144 die("file write error");
151 ssize_t
write_file(int fd
, const void *buf
, size_t count
)
157 dprintf("write_file(%d,%p,%u)\n", fd
, buf
, count
);
160 asm volatile ("int $0x21 ; setc %0":"=bcdm" (err
), "=a"(rv
)
161 :"a"(0x4000), "b"(fd
), "c"(count
), "d"(buf
));
163 die("file write error");
172 static inline __attribute__ ((const))
173 uint16_t data_segment(void)
177 asm("movw %%ds,%0" : "=rm"(ds
));
181 void write_device(int drive
, const void *buf
, size_t nsecs
, unsigned int sector
)
186 dprintf("write_device(%d,%p,%u,%u)\n", drive
, buf
, nsecs
, sector
);
188 dio
.startsector
= sector
;
190 dio
.bufoffs
= (uintptr_t) buf
;
191 dio
.bufseg
= data_segment();
193 /* Try FAT32-aware system call first */
194 asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n"
197 : "a" (0x7305), "b" (&dio
), "c" (-1), "d" (drive
),
201 /* If not supported, try the legacy system call (int2526.S) */
202 if (errnum
== 0x0001)
203 errnum
= int26_write_sector(drive
, &dio
);
206 dprintf("rv = %04x\n", errnum
);
207 die("sector write error");
211 void read_device(int drive
, const void *buf
, size_t nsecs
, unsigned int sector
)
216 dprintf("read_device(%d,%p,%u,%u)\n", drive
, buf
, nsecs
, sector
);
218 dio
.startsector
= sector
;
220 dio
.bufoffs
= (uintptr_t) buf
;
221 dio
.bufseg
= data_segment();
223 /* Try FAT32-aware system call first */
224 asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n"
227 : "a" (0x7305), "b" (&dio
), "c" (-1), "d" (drive
),
230 /* If not supported, try the legacy system call (int2526.S) */
231 if (errnum
== 0x0001)
232 errnum
= int25_read_sector(drive
, &dio
);
235 dprintf("rv = %04x\n", errnum
);
236 die("sector read error");
240 /* Both traditional DOS and FAT32 DOS return this structure, but
241 FAT32 return a lot more data, so make sure we have plenty of space */
242 struct deviceparams
{
248 uint16_t bytespersec
;
252 uint16_t rootdirents
;
256 uint16_t secpertrack
;
259 uint32_t hugesectors
;
260 uint8_t lotsofpadding
[224];
261 } __attribute__ ((packed
));
263 uint32_t get_partition_offset(int drive
)
267 struct deviceparams dp
;
269 dp
.specfunc
= 1; /* Get current information */
272 asm volatile ("int $0x21 ; setc %0"
273 :"=abcdm" (err
), "+a"(rv
), "=m"(dp
)
274 :"b" (drive
), "c" (0x0860), "d" (&dp
));
277 return dp
.hiddensecs
;
280 asm volatile ("int $0x21 ; setc %0"
281 : "=abcdm" (err
), "+a" (rv
), "=m" (dp
)
282 : "b" (drive
), "c" (0x4860), "d" (&dp
));
285 return dp
.hiddensecs
;
287 die("could not find partition start offset");
294 uint16_t firstsector
;
296 uint16_t bufferoffset
;
298 } __attribute__ ((packed
));
300 static struct rwblock mbr
= {
304 .firstsector
= 0, /* MS-DOS, unlike the BIOS, zero-base sectors */
310 void write_mbr(int drive
, const void *buf
)
315 dprintf("write_mbr(%d,%p)", drive
, buf
);
317 mbr
.bufferoffset
= (uintptr_t) buf
;
318 mbr
.bufferseg
= data_segment();
321 asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err
), "+a"(rv
)
322 :"c"(0x0841), "d"(&mbr
), "b"(drive
), "m"(mbr
));
324 dprintf(" rv(0841) = %04x", rv
);
331 asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err
), "+a"(rv
)
332 :"c"(0x4841), "d"(&mbr
), "b"(drive
), "m"(mbr
));
334 dprintf(" rv(4841) = %04x\n", rv
);
336 die("mbr write error");
339 void read_mbr(int drive
, const void *buf
)
344 dprintf("read_mbr(%d,%p)", drive
, buf
);
346 mbr
.bufferoffset
= (uintptr_t) buf
;
347 mbr
.bufferseg
= data_segment();
350 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err
), "+a"(rv
)
351 :"c"(0x0861), "d"(&mbr
), "b"(drive
), "m"(mbr
));
353 dprintf(" rv(0861) = %04x", rv
);
360 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err
), "+a"(rv
)
361 :"c"(0x4861), "d"(&mbr
), "b"(drive
), "m"(mbr
));
363 dprintf(" rv(4841) = %04x\n", rv
);
365 die("mbr read error");
367 dprintf("Bytes: %02x %02x %02x %02x %02x %02x %02x %02x\n",
368 ((const uint8_t *)buf
)[0],
369 ((const uint8_t *)buf
)[1],
370 ((const uint8_t *)buf
)[2],
371 ((const uint8_t *)buf
)[3],
372 ((const uint8_t *)buf
)[4],
373 ((const uint8_t *)buf
)[5],
374 ((const uint8_t *)buf
)[6],
375 ((const uint8_t *)buf
)[7]);
378 /* This call can legitimately fail, and we don't care, so ignore error return */
379 void set_attributes(const char *file
, int attributes
)
381 uint16_t rv
= 0x4301;
383 dprintf("set_attributes(\"%s\", 0x%02x)\n", file
, attributes
);
385 asm volatile ("int $0x21":"+a" (rv
)
386 :"c"(attributes
), "d"(file
));
390 * Version of the read_device function suitable for libfat
392 int libfat_xpread(intptr_t pp
, void *buf
, size_t secsize
,
393 libfat_sector_t sector
)
395 read_device(pp
, buf
, 1, sector
);
399 static inline void get_dos_version(void)
403 asm("int $0x21 ; xchgb %%ah,%%al"
409 dprintf("DOS version %d.%d\n", (dos_version
>> 8), dos_version
& 0xff);
412 /* The locking interface relies on static variables. A massive hack :( */
413 static uint8_t lock_level
, lock_drive
;
415 static inline void set_lock_device(uint8_t device
)
421 static int do_lock(uint8_t level
)
423 uint16_t level_arg
= lock_drive
+ (level
<< 8);
427 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
428 uint16_t lock_call
= (dos_version
>= 0x070a) ? 0x484A : 0x084A;
430 uint16_t lock_call
= 0x084A; /* MSDN says this is OK for all filesystems */
433 dprintf("Trying lock %04x... ", level_arg
);
434 asm volatile ("int $0x21 ; setc %0"
435 : "=bcdm" (err
), "=a" (rv
)
436 : "a" (0x440d), "b" (level_arg
),
437 "c" (lock_call
), "d" (0x0001));
438 dprintf("%s %04x\n", err
? "err" : "ok", rv
);
443 void lock_device(int level
)
445 static int hard_lock
= 0;
448 if (dos_version
< 0x0700)
449 return; /* Win9x/NT only */
452 /* Assume hierarchial "soft" locking supported */
454 while (lock_level
< level
) {
455 int new_level
= lock_level
+ 1;
456 err
= do_lock(new_level
);
459 /* Try hard locking next */
465 lock_level
= new_level
;
472 /* Hard locking, only level 4 supported */
473 /* This is needed for Win9x in DOS mode */
478 /* Assume locking is not needed */
489 die("could not lock device");
492 void unlock_device(int level
)
496 uint16_t unlock_call
;
498 if (dos_version
< 0x0700)
499 return; /* Win9x/NT only */
502 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
503 unlock_call
= (dos_version
>= 0x070a) ? 0x486A : 0x086A;
505 unlock_call
= 0x086A; /* MSDN says this is OK for all filesystems */
508 if (lock_level
== 4 && level
> 0)
509 return; /* Only drop the hard lock at the end */
511 while (lock_level
> level
) {
512 uint8_t new_level
= (lock_level
== 4) ? 0 : lock_level
- 1;
513 uint16_t level_arg
= (new_level
<< 8) + lock_drive
;
515 dprintf("Trying unlock %04x... ", new_level
);
516 asm volatile ("int $0x21 ; setc %0"
517 : "=bcdm" (err
), "+a" (rv
)
518 : "b" (level_arg
), "c" (unlock_call
));
519 dprintf("%s %04x\n", err
? "err" : "ok", rv
);
520 lock_level
= new_level
;
525 * This function does any desired MBR manipulation; called with the device lock held.
528 uint8_t active
; /* Active flag */
529 uint8_t bhead
; /* Begin head */
530 uint8_t bsector
; /* Begin sector */
531 uint8_t bcylinder
; /* Begin cylinder */
532 uint8_t filesystem
; /* Filesystem value */
533 uint8_t ehead
; /* End head */
534 uint8_t esector
; /* End sector */
535 uint8_t ecylinder
; /* End cylinder */
536 uint32_t startlba
; /* Start sector LBA */
537 uint32_t sectors
; /* Length in sectors */
538 } __attribute__ ((packed
));
540 static void adjust_mbr(int device
, int writembr
, int set_active
)
542 static unsigned char sectbuf
[SECTOR_SIZE
];
545 if (!writembr
&& !set_active
)
546 return; /* Nothing to do */
548 read_mbr(device
, sectbuf
);
551 memcpy(sectbuf
, syslinux_mbr
, syslinux_mbr_len
);
552 *(uint16_t *) (sectbuf
+ 510) = 0xaa55;
556 uint32_t offset
= get_partition_offset(device
);
557 struct mbr_entry
*me
= (struct mbr_entry
*)(sectbuf
+ 446);
560 dprintf("Searching for partition offset: %08x\n", offset
);
562 for (i
= 0; i
< 4; i
++) {
563 if (me
->startlba
== offset
) {
573 die("partition not found (-a is not implemented for logical partitions)");
574 } else if (found
> 1) {
575 die("multiple aliased partitions found");
579 write_mbr(device
, sectbuf
);
582 int main(int argc
, char *argv
[])
584 static unsigned char sectbuf
[SECTOR_SIZE
];
586 static char ldlinux_name
[] = "@:\\ldlinux.sys";
588 int force
= 0; /* -f (force) option */
589 struct libfat_filesystem
*fs
;
590 libfat_sector_t s
, *secp
;
591 libfat_sector_t
*sectors
;
593 int32_t ldlinux_cluster
;
595 const char *device
= NULL
, *bootsecfile
= NULL
;
598 int writembr
= 0; /* -m (write MBR) option */
599 int set_active
= 0; /* -a (set partition active) option */
600 const char *subdir
= NULL
;
605 ldlinux_seg
= (size_t) __payload_sseg
+ data_segment();
607 dprintf("argv = %p\n", argv
);
608 for (i
= 0; i
<= argc
; i
++)
609 dprintf("argv[%d] = %p = \"%s\"\n", i
, argv
[i
], argv
[i
]);
611 (void)argc
; /* Unused */
615 for (argp
= argv
+ 1; *argp
; argp
++) {
623 case 's': /* Use "safe, slow and stupid" code */
626 case 'r': /* RAID mode */
629 case 'f': /* Force install */
632 case 'm': /* Write MBR */
635 case 'a': /* Set partition active */
661 * Figure out which drive we're talking to
663 dev_fd
= (device
[0] & ~0x20) - 0x40;
664 if (dev_fd
< 1 || dev_fd
> 26 || device
[1] != ':' || device
[2])
667 set_lock_device(dev_fd
);
669 lock_device(2); /* Make sure we can lock the device */
670 read_device(dev_fd
, sectbuf
, 1, 0);
674 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
676 if ((errmsg
= syslinux_check_bootsect(sectbuf
))) {
683 ldlinux_name
[0] = dev_fd
| 0x40;
685 set_attributes(ldlinux_name
, 0);
686 fd
= creat(ldlinux_name
, 0); /* SYSTEM HIDDEN READONLY */
689 set_attributes(ldlinux_name
, 0x07); /* SYSTEM HIDDEN READONLY */
692 * Now, use libfat to create a block map. This probably
693 * should be changed to use ioctl(...,FIBMAP,...) since
694 * this is supposed to be a simple, privileged version
697 ldlinux_sectors
= (syslinux_ldlinux_len
+ SECTOR_SIZE
- 1) >> SECTOR_SHIFT
;
698 sectors
= calloc(ldlinux_sectors
, sizeof *sectors
);
700 fs
= libfat_open(libfat_xpread
, dev_fd
);
701 ldlinux_cluster
= libfat_searchdir(fs
, 0, "LDLINUX SYS", NULL
);
704 s
= libfat_clustertosector(fs
, ldlinux_cluster
);
705 while (s
&& nsectors
< ldlinux_sectors
) {
708 s
= libfat_nextsector(fs
, s
);
713 * If requested, move ldlinux.sys
716 char new_ldlinux_name
[160];
717 char *cp
= new_ldlinux_name
+ 3;
721 new_ldlinux_name
[0] = dev_fd
| 0x40;
722 new_ldlinux_name
[1] = ':';
723 new_ldlinux_name
[2] = '\\';
725 for (sd
= subdir
; *sd
; sd
++) {
728 if (c
== '/' || c
== '\\') {
740 /* Skip if subdirectory == root */
741 if (cp
> new_ldlinux_name
+ 3) {
745 memcpy(cp
, "ldlinux.sys", 12);
747 set_attributes(ldlinux_name
, 0);
748 if (rename(ldlinux_name
, new_ldlinux_name
))
749 set_attributes(ldlinux_name
, 0x07);
751 set_attributes(new_ldlinux_name
, 0x07);
756 * Patch ldlinux.sys and the boot sector
758 i
= syslinux_patch(sectors
, nsectors
, stupid
, raid_mode
, subdir
, NULL
);
759 patch_sectors
= (i
+ SECTOR_SIZE
- 1) >> SECTOR_SHIFT
;
762 * Overwrite the now-patched ldlinux.sys
764 /* lock_device(3); -- doesn't seem to be needed */
765 for (i
= 0; i
< patch_sectors
; i
++) {
768 di
= (size_t) sectbuf
;
769 cx
= SECTOR_SIZE
>> 2;
770 asm volatile ("movw %3,%%fs ; fs ; rep ; movsl":"+S" (si
), "+D"(di
),
773 (ldlinux_seg
+ (i
<< (SECTOR_SHIFT
- 4)))));
774 write_device(dev_fd
, sectbuf
, 1, sectors
[i
]);
778 * Muck with the MBR, if desired, while we hold the lock
780 adjust_mbr(dev_fd
, writembr
, set_active
);
783 * To finish up, write the boot sector
786 /* Read the superblock again since it might have changed while mounted */
787 read_device(dev_fd
, sectbuf
, 1, 0);
789 /* Copy the syslinux code into the boot sector */
790 syslinux_make_bootsect(sectbuf
);
792 /* Write new boot sector */
795 fd
= creat(bootsecfile
, 0x20); /* ARCHIVE */
796 write_file(fd
, sectbuf
, SECTOR_SIZE
);
799 write_device(dev_fd
, sectbuf
, 1, 0);