Before this fix, the cpu flags were shifted
[syslinux.git] / extlinux / extlinux.c
blob75de5b9ffa6960b362ad291e1d48046925506367
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1998-2005 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 * extlinux.c
16 * Install the extlinux boot block on an ext2/3 filesystem
19 #define _GNU_SOURCE /* Enable everything */
20 #include <inttypes.h>
21 /* This is needed to deal with the kernel headers imported into glibc 3.3.3. */
22 typedef uint64_t u64;
23 #include <alloca.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #ifndef __KLIBC__
29 #include <mntent.h>
30 #endif
31 #include <stdlib.h>
32 #include <string.h>
33 #include <getopt.h>
34 #include <sysexits.h>
35 #include <sys/ioctl.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <sys/mount.h>
40 #include <linux/fd.h> /* Floppy geometry */
41 #include <linux/hdreg.h> /* Hard disk geometry */
42 #include <linux/fs.h> /* FIGETBSZ, FIBMAP */
44 #include "ext2_fs.h"
45 #include "../version.h"
47 #ifdef DEBUG
48 # define dprintf printf
49 #else
50 # define dprintf(...) ((void)0)
51 #endif
53 /* Global option handling */
55 const char *program;
57 /* These are the options we can set and their values */
58 struct my_options {
59 unsigned int sectors;
60 unsigned int heads;
61 } opt = {
62 .sectors = 0,
63 .heads = 0,
66 static void __attribute__((noreturn)) usage(int rv)
68 fprintf(stderr,
69 "Usage: %s [options] directory\n"
70 " --install -i Install over the current bootsector\n"
71 " --update -U Update a previous EXTLINUX installation\n"
72 " --zip -z Force zipdrive geometry (-H 64 -S 32)\n"
73 " --sectors=# -S Force the number of sectors per track\n"
74 " --heads=# -H Force number of heads\n"
75 "\n"
76 " Note: geometry is determined at boot time for devices which\n"
77 " are considered hard disks by the BIOS. Unfortunately, this is\n"
78 " not possible for devices which are considered floppy disks,\n"
79 " which includes zipdisks and LS-120 superfloppies.\n"
80 "\n"
81 " The -z option is useful for USB devices which are considered\n"
82 " hard disks by some BIOSes and zipdrives by other BIOSes.\n",
83 program);
85 exit(rv);
88 static const struct option long_options[] = {
89 { "install", 0, NULL, 'i' },
90 { "update", 0, NULL, 'U' },
91 { "zipdrive", 0, NULL, 'z' },
92 { "sectors", 1, NULL, 'S' },
93 { "heads", 1, NULL, 'H' },
94 { "version", 0, NULL, 'v' },
95 { "help", 0, NULL, 'h' },
96 { 0, 0, 0, 0 }
99 static const char short_options[] = "iUuzS:H:vh";
103 #if defined(__linux__) && !defined(BLKGETSIZE64)
104 /* This takes a u64, but the size field says size_t. Someone screwed big. */
105 # define BLKGETSIZE64 _IOR(0x12,114,size_t)
106 #endif
108 #define LDLINUX_MAGIC 0x3eb202fe
110 enum bs_offsets {
111 bsJump = 0x00,
112 bsOemName = 0x03,
113 bsBytesPerSec = 0x0b,
114 bsSecPerClust = 0x0d,
115 bsResSectors = 0x0e,
116 bsFATs = 0x10,
117 bsRootDirEnts = 0x11,
118 bsSectors = 0x13,
119 bsMedia = 0x15,
120 bsFATsecs = 0x16,
121 bsSecPerTrack = 0x18,
122 bsHeads = 0x1a,
123 bsHiddenSecs = 0x1c,
124 bsHugeSectors = 0x20,
126 /* FAT12/16 only */
127 bs16DriveNumber = 0x24,
128 bs16Reserved1 = 0x25,
129 bs16BootSignature = 0x26,
130 bs16VolumeID = 0x27,
131 bs16VolumeLabel = 0x2b,
132 bs16FileSysType = 0x36,
133 bs16Code = 0x3e,
135 /* FAT32 only */
136 bs32FATSz32 = 36,
137 bs32ExtFlags = 40,
138 bs32FSVer = 42,
139 bs32RootClus = 44,
140 bs32FSInfo = 48,
141 bs32BkBootSec = 50,
142 bs32Reserved = 52,
143 bs32DriveNumber = 64,
144 bs32Reserved1 = 65,
145 bs32BootSignature = 66,
146 bs32VolumeID = 67,
147 bs32VolumeLabel = 71,
148 bs32FileSysType = 82,
149 bs32Code = 90,
151 bsSignature = 0x1fe
154 #define bsHead bsJump
155 #define bsHeadLen (bsOemName-bsHead)
156 #define bsCode bs32Code /* The common safe choice */
157 #define bsCodeLen (bsSignature-bs32Code)
160 * Access functions for littleendian numbers, possibly misaligned.
162 static inline uint8_t get_8(const unsigned char *p)
164 return *(const uint8_t *)p;
167 static inline uint16_t get_16(const unsigned char *p)
169 #if defined(__i386__) || defined(__x86_64__)
170 /* Littleendian and unaligned-capable */
171 return *(const uint16_t *)p;
172 #else
173 return (uint16_t)p[0] + ((uint16_t)p[1] << 8);
174 #endif
177 static inline uint32_t get_32(const unsigned char *p)
179 #if defined(__i386__) || defined(__x86_64__)
180 /* Littleendian and unaligned-capable */
181 return *(const uint32_t *)p;
182 #else
183 return (uint32_t)p[0] + ((uint32_t)p[1] << 8) +
184 ((uint32_t)p[2] << 16) + ((uint32_t)p[3] << 24);
185 #endif
188 static inline void set_16(unsigned char *p, uint16_t v)
190 #if defined(__i386__) || defined(__x86_64__)
191 /* Littleendian and unaligned-capable */
192 *(uint16_t *)p = v;
193 #else
194 p[0] = (v & 0xff);
195 p[1] = ((v >> 8) & 0xff);
196 #endif
199 static inline void set_32(unsigned char *p, uint32_t v)
201 #if defined(__i386__) || defined(__x86_64__)
202 /* Littleendian and unaligned-capable */
203 *(uint32_t *)p = v;
204 #else
205 p[0] = (v & 0xff);
206 p[1] = ((v >> 8) & 0xff);
207 p[2] = ((v >> 16) & 0xff);
208 p[3] = ((v >> 24) & 0xff);
209 #endif
212 #ifndef EXT2_SUPER_OFFSET
213 #define EXT2_SUPER_OFFSET 1024
214 #endif
216 #define SECTOR_SHIFT 9 /* 512-byte sectors */
217 #define SECTOR_SIZE (1 << SECTOR_SHIFT)
219 const char *program;
222 * Boot block
224 extern unsigned char extlinux_bootsect[];
225 extern unsigned int extlinux_bootsect_len;
226 #define boot_block extlinux_bootsect
227 #define boot_block_len extlinux_bootsect_len
230 * Image file
232 extern unsigned char extlinux_image[];
233 extern unsigned int extlinux_image_len;
234 #define boot_image extlinux_image
235 #define boot_image_len extlinux_image_len
238 * Common abort function
240 void __attribute__((noreturn)) die(const char *msg)
242 fputs(msg, stderr);
243 exit(1);
247 * read/write wrapper functions
249 ssize_t xpread(int fd, void *buf, size_t count, off_t offset)
251 char *bufp = (char *)buf;
252 ssize_t rv;
253 ssize_t done = 0;
255 while ( count ) {
256 rv = pread(fd, bufp, count, offset);
257 if ( rv == 0 ) {
258 die("short read");
259 } else if ( rv == -1 ) {
260 if ( errno == EINTR ) {
261 continue;
262 } else {
263 die(strerror(errno));
265 } else {
266 bufp += rv;
267 offset += rv;
268 done += rv;
269 count -= rv;
273 return done;
276 ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset)
278 const char *bufp = (const char *)buf;
279 ssize_t rv;
280 ssize_t done = 0;
282 while ( count ) {
283 rv = pwrite(fd, bufp, count, offset);
284 if ( rv == 0 ) {
285 die("short write");
286 } else if ( rv == -1 ) {
287 if ( errno == EINTR ) {
288 continue;
289 } else {
290 die(strerror(errno));
292 } else {
293 bufp += rv;
294 offset += rv;
295 done += rv;
296 count -= rv;
300 return done;
304 * Produce file map
307 sectmap(int fd, uint32_t *sectors, int nsectors)
309 unsigned int blksize, blk, nblk;
310 unsigned int i;
312 /* Get block size */
313 if ( ioctl(fd, FIGETBSZ, &blksize) )
314 return -1;
316 /* Number of sectors per block */
317 blksize >>= SECTOR_SHIFT;
319 nblk = 0;
320 while ( nsectors ) {
322 blk = nblk++;
323 dprintf("querying block %u\n", blk);
324 if ( ioctl(fd, FIBMAP, &blk) )
325 return -1;
327 blk *= blksize;
328 for ( i = 0 ; i < blksize ; i++ ) {
329 if ( !nsectors )
330 return 0;
332 dprintf("Sector: %10u\n", blk);
333 *sectors++ = blk++;
334 nsectors--;
338 return 0;
342 * Get the size of a block device
344 uint64_t get_size(int devfd)
346 uint64_t bytes;
347 uint32_t sects;
348 struct stat st;
350 #ifdef BLKGETSIZE64
351 if ( !ioctl(devfd, BLKGETSIZE64, &bytes) )
352 return bytes;
353 #endif
354 if ( !ioctl(devfd, BLKGETSIZE, &sects) )
355 return (uint64_t)sects << 9;
356 else if ( !fstat(devfd, &st) && st.st_size )
357 return st.st_size;
358 else
359 return 0;
364 * Get device geometry and partition offset
366 struct geometry_table {
367 uint64_t bytes;
368 struct hd_geometry g;
371 /* Standard floppy disk geometries, plus LS-120. Zipdisk geometry
372 (x/64/32) is the final fallback. I don't know what LS-240 has
373 as its geometry, since I don't have one and don't know anyone that does,
374 and Google wasn't helpful... */
375 static const struct geometry_table standard_geometries[] = {
376 { 360*1024, { 2, 9, 40, 0 } },
377 { 720*1024, { 2, 9, 80, 0 } },
378 { 1200*1024, { 2, 15, 80, 0 } },
379 { 1440*1024, { 2, 18, 80, 0 } },
380 { 1680*1024, { 2, 21, 80, 0 } },
381 { 1722*1024, { 2, 21, 80, 0 } },
382 { 2880*1024, { 2, 36, 80, 0 } },
383 { 3840*1024, { 2, 48, 80, 0 } },
384 { 123264*1024, { 8, 32, 963, 0 } }, /* LS120 */
385 { 0, {0,0,0,0} }
389 get_geometry(int devfd, uint64_t totalbytes, struct hd_geometry *geo)
391 struct floppy_struct fd_str;
392 const struct geometry_table *gp;
394 memset(geo, 0, sizeof *geo);
396 if ( !ioctl(devfd, HDIO_GETGEO, &geo) ) {
397 return 0;
398 } else if ( !ioctl(devfd, FDGETPRM, &fd_str) ) {
399 geo->heads = fd_str.head;
400 geo->sectors = fd_str.sect;
401 geo->cylinders = fd_str.track;
402 geo->start = 0;
403 return 0;
406 /* Didn't work. Let's see if this is one of the standard geometries */
407 for ( gp = standard_geometries ; gp->bytes ; gp++ ) {
408 if ( gp->bytes == totalbytes ) {
409 memcpy(geo, &gp->g, sizeof *geo);
410 return 0;
414 /* Didn't work either... assign a geometry of 64 heads, 32 sectors; this is
415 what zipdisks use, so this would help if someone has a USB key that
416 they're booting in USB-ZIP mode. */
418 geo->heads = opt.heads ?: 64;
419 geo->sectors = opt.sectors ?: 32;
420 geo->cylinders = totalbytes/(geo->heads*geo->sectors << SECTOR_SHIFT);
421 geo->start = 0;
423 if ( !opt.sectors && !opt.heads )
424 fprintf(stderr, "Warning: unable to obtain device geometry (defaulting to %d heads, %d sectors)\n"
425 " (on hard disks, this is usually harmless.)\n",
426 geo->heads, geo->sectors);
428 return 1;
432 * Query the device geometry and put it into the boot sector.
433 * Map the file and put the map in the boot sector and file.
434 * Stick the "current directory" inode number into the file.
436 void
437 patch_file_and_bootblock(int fd, int dirfd, int devfd)
439 struct stat dirst;
440 struct hd_geometry geo;
441 uint32_t *sectp;
442 uint64_t totalbytes, totalsectors;
443 int nsect;
444 unsigned char *p, *patcharea;
445 int i, dw;
446 uint32_t csum;
448 if ( fstat(dirfd, &dirst) ) {
449 perror("fstat dirfd");
450 exit(255); /* This should never happen */
453 totalbytes = get_size(devfd);
454 get_geometry(devfd, totalbytes, &geo);
456 if ( opt.heads )
457 geo.heads = opt.heads;
458 if ( opt.sectors )
459 geo.sectors = opt.sectors;
461 /* Patch this into a fake FAT superblock. This isn't because
462 FAT is a good format in any way, it's because it lets the
463 early bootstrap share code with the FAT version. */
464 dprintf("heads = %u, sect = %u\n", geo.heads, geo.sectors);
466 totalsectors = totalbytes >> SECTOR_SHIFT;
467 if ( totalsectors >= 65536 ) {
468 set_16(boot_block+bsSectors, 0);
469 } else {
470 set_16(boot_block+bsSectors, totalsectors);
472 set_32(boot_block+bsHugeSectors, totalsectors);
474 set_16(boot_block+bsBytesPerSec, SECTOR_SIZE);
475 set_16(boot_block+bsSecPerTrack, geo.sectors);
476 set_16(boot_block+bsHeads, geo.heads);
477 set_32(boot_block+bsHiddenSecs, geo.start);
479 /* Construct the boot file */
481 dprintf("directory inode = %lu\n", (unsigned long) dirst.st_ino);
482 nsect = (boot_image_len+SECTOR_SIZE-1) >> SECTOR_SHIFT;
483 sectp = alloca(sizeof(uint32_t)*nsect);
484 if ( sectmap(fd, sectp, nsect) ) {
485 perror("bmap");
486 exit(1);
489 /* First sector need pointer in boot sector */
490 set_32(boot_block+0x1F8, *sectp++);
491 nsect--;
493 /* Search for LDLINUX_MAGIC to find the patch area */
494 for ( p = boot_image ; get_32(p) != LDLINUX_MAGIC ; p += 4 );
495 patcharea = p+8;
497 /* Set up the totals */
498 dw = boot_image_len >> 2; /* COMPLETE dwords! */
499 set_16(patcharea, dw);
500 set_16(patcharea+2, nsect); /* Does not include the first sector! */
501 set_32(patcharea+8, dirst.st_ino); /* "Current" directory */
503 /* Set the sector pointers */
504 p = patcharea+12;
506 memset(p, 0, 64*4);
507 while ( nsect-- ) {
508 set_32(p, *sectp++);
509 p += 4;
512 /* Now produce a checksum */
513 set_32(patcharea+4, 0);
515 csum = LDLINUX_MAGIC;
516 for ( i = 0, p = boot_image ; i < dw ; i++, p += 4 )
517 csum -= get_32(p); /* Negative checksum */
519 set_32(patcharea+4, csum);
523 * Install the boot block on the specified device.
524 * Must be run AFTER install_file()!
527 install_bootblock(int fd, const char *device)
529 struct ext2_super_block sb;
531 if ( xpread(fd, &sb, sizeof sb, EXT2_SUPER_OFFSET) != sizeof sb ) {
532 perror("reading superblock");
533 return 1;
536 if ( sb.s_magic != EXT2_SUPER_MAGIC ) {
537 fprintf(stderr, "no ext2/ext3 superblock found on %s\n", device);
538 return 1;
541 if ( xpwrite(fd, boot_block, boot_block_len, 0) != boot_block_len ) {
542 perror("writing bootblock");
543 return 1;
546 return 0;
550 install_file(const char *path, int devfd, struct stat *rst)
552 char *file;
553 int fd = -1, dirfd = -1, flags;
554 struct stat st;
556 asprintf(&file, "%s%sextlinux.sys",
557 path,
558 path[0] && path[strlen(path)-1] == '/' ? "" : "/");
559 if ( !file ) {
560 perror(program);
561 return 1;
564 dirfd = open(path, O_RDONLY|O_DIRECTORY);
565 if ( dirfd < 0 ) {
566 perror(path);
567 goto bail;
570 fd = open(file, O_RDONLY);
571 if ( fd < 0 ) {
572 if ( errno != ENOENT ) {
573 perror(file);
574 goto bail;
576 } else {
577 /* If file exist, remove the immutable flag and set u+w mode */
578 if ( !ioctl(fd, EXT2_IOC_GETFLAGS, &flags) ) {
579 flags &= ~EXT2_IMMUTABLE_FL;
580 ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
582 if ( !fstat(fd, &st) ) {
583 fchmod(fd, st.st_mode | S_IWUSR);
586 close(fd);
588 fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, S_IRUSR|S_IRGRP|S_IROTH);
589 if ( fd < 0 ) {
590 perror(file);
591 goto bail;
594 /* Write it the first time */
595 if ( xpwrite(fd, boot_image, boot_image_len, 0) != boot_image_len ) {
596 fprintf(stderr, "%s: write failure on %s\n", program, file);
597 goto bail;
600 /* Map the file, and patch the initial sector accordingly */
601 patch_file_and_bootblock(fd, dirfd, devfd);
603 /* Write it again - this relies on the file being overwritten in place! */
604 if ( xpwrite(fd, boot_image, boot_image_len, 0) != boot_image_len ) {
605 fprintf(stderr, "%s: write failure on %s\n", program, file);
606 goto bail;
609 /* Attempt to set immutable flag and remove all write access */
610 /* Only set immutable flag if file is owned by root */
611 if ( !fstat(fd, &st) ) {
612 fchmod(fd, st.st_mode & (S_IRUSR|S_IRGRP|S_IROTH));
613 if ( st.st_uid == 0 && !ioctl(fd, EXT2_IOC_GETFLAGS, &flags) ) {
614 flags |= EXT2_IMMUTABLE_FL;
615 ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
619 if ( fstat(fd, rst) ) {
620 perror(file);
621 goto bail;
624 close(dirfd);
625 close(fd);
626 return 0;
628 bail:
629 if ( dirfd >= 0 )
630 close(dirfd);
631 if ( fd >= 0 )
632 close(fd);
634 return 1;
637 /* EXTLINUX installs the string 'EXTLINUX' at offset 3 in the boot
638 sector; this is consistent with FAT filesystems. */
640 already_installed(int devfd)
642 char buffer[8];
644 xpread(devfd, buffer, 8, 3);
645 return !memcmp(buffer, "EXTLINUX", 8);
649 #ifdef __KLIBC__
650 static char devname_buf[64];
652 static void device_cleanup(void)
654 unlink(devname_buf);
656 #endif
660 install_loader(const char *path, int update_only)
662 struct stat st, fst;
663 int devfd, rv;
664 const char *devname = NULL;
665 #ifndef __KLIBC__
666 struct mntent *mnt = NULL;
667 struct stat dst;
668 FILE *mtab;
669 #endif
671 if ( stat(path, &st) || !S_ISDIR(st.st_mode) ) {
672 fprintf(stderr, "%s: Not a directory: %s\n", program, path);
673 return 1;
676 devfd = -1;
678 #ifdef __KLIBC__
680 /* klibc doesn't have getmntent and friends; instead, just create
681 a new device with the appropriate device type */
682 snprintf(devname_buf, sizeof devname_buf, "/tmp/dev-%u:%u",
683 major(st.st_dev), minor(st.st_dev));
685 if (mknod(devname_buf, S_IFBLK|0600, st.st_dev)) {
686 fprintf(stderr, "%s: cannot create device %s\n", program, devname);
687 return 1;
690 atexit(device_cleanup); /* unlink the device node on exit */
691 devname = devname_buf;
693 #else
695 if ( (mtab = setmntent("/proc/mounts", "r")) ) {
696 while ( (mnt = getmntent(mtab)) ) {
697 if ( (!strcmp(mnt->mnt_type, "ext2") ||
698 !strcmp(mnt->mnt_type, "ext3")) &&
699 !stat(mnt->mnt_fsname, &dst) &&
700 dst.st_rdev == st.st_dev ) {
701 devname = mnt->mnt_fsname;
702 break;
707 if ( !devname ) {
708 /* Didn't find it in /proc/mounts, try /etc/mtab */
709 if ( (mtab = setmntent("/etc/mtab", "r")) ) {
710 while ( (mnt = getmntent(mtab)) ) {
711 devname = mnt->mnt_fsname;
712 break;
717 if ( !devname ) {
718 fprintf(stderr, "%s: cannot find device for path %s\n", program, path);
719 return 1;
722 fprintf(stderr, "%s is device %s\n", path, devname);
724 #endif
726 if ( (devfd = open(devname, O_RDWR|O_SYNC)) < 0 ) {
727 fprintf(stderr, "%s: cannot open device %s\n", program, devname);
728 return 1;
731 if ( update_only && !already_installed(devfd) ) {
732 fprintf(stderr, "%s: no previous extlinux boot sector found\n", program);
733 return 1;
736 install_file(path, devfd, &fst);
738 if ( fst.st_dev != st.st_dev ) {
739 fprintf(stderr, "%s: file system changed under us - aborting!\n",
740 program);
741 return 1;
744 sync();
745 rv = install_bootblock(devfd, devname);
746 close(devfd);
747 sync();
749 return rv;
753 main(int argc, char *argv[])
755 int o;
756 const char *directory;
757 int update_only = -1;
759 program = argv[0];
761 while ( (o = getopt_long(argc, argv, short_options,
762 long_options, NULL)) != EOF ) {
763 switch ( o ) {
764 case 'z':
765 opt.heads = 64;
766 opt.sectors = 32;
767 break;
768 case 'S':
769 opt.sectors = strtoul(optarg, NULL, 0);
770 if ( opt.sectors < 1 || opt.sectors > 63 ) {
771 fprintf(stderr, "%s: invalid number of sectors: %u (must be 1-63)\n",
772 program, opt.sectors);
773 exit(EX_USAGE);
775 break;
776 case 'H':
777 opt.heads = strtoul(optarg, NULL, 0);
778 if ( opt.heads < 1 || opt.heads > 256 ) {
779 fprintf(stderr, "%s: invalid number of heads: %u (must be 1-256)\n",
780 program, opt.heads);
781 exit(EX_USAGE);
783 break;
784 case 'i':
785 update_only = 0;
786 break;
787 case 'u':
788 case 'U':
789 update_only = 1;
790 break;
791 case 'h':
792 usage(0);
793 break;
794 case 'v':
795 fputs("extlinux " VERSION "\n", stderr);
796 exit(0);
797 default:
798 usage(EX_USAGE);
802 directory = argv[optind];
804 if ( !directory )
805 usage(EX_USAGE);
807 if ( update_only == -1 ) {
808 fprintf(stderr, "%s: warning: a future version will require --install or --update\n",
809 program);
810 update_only = 0;
813 return install_loader(directory, update_only);