2 /* ----------------------------------------------------------------------- *
4 * Copyright 1998-2004 H. Peter Anvin - All Rights Reserved
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
17 * This program ought to be portable. I hope so, at least.
19 * This is an alternate version of the installer which doesn't require
20 * mtools, but requires root privilege.
24 # define DO_DIRECT_MOUNT 1 /* Call mount(2) directly */
26 # define DO_DIRECT_MOUNT 0 /* Call /bin/mount instead */
30 #define _XOPEN_SOURCE 500 /* For pread() pwrite() */
31 #define _FILE_OFFSET_BITS 64
42 #include <sys/types.h>
44 #include <sys/mount.h>
51 # include <linux/loop.h>
57 # define _PATH_MOUNT "/bin/mount"
60 # define _PATH_UMOUNT "/bin/umount"
65 const char *program; /* Name of program */
66 const char *device; /* Device to install to */
68 char *mntpath = NULL; /* Path on which to mount */
69 off_t filesystem_offset = 0; /* Filesystem offset */
71 int loop_fd = -1; /* Loop device */
74 void __attribute__((noreturn)) usage(void)
76 fprintf(stderr, "Usage: %s [-sf] [-o offset] device\n", program);
80 void __attribute__((noreturn)) die(const char *msg)
82 fprintf(stderr, "%s: %s\n", program, msg);
85 if ( loop_fd != -1 ) {
86 ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
99 * read/write wrapper functions
101 ssize_t xpread(int fd, void *buf, size_t count, off_t offset)
107 rv = pread(fd, buf, count, offset);
110 } else if ( rv == -1 ) {
111 if ( errno == EINTR ) {
127 ssize_t xpwrite(int fd, void *buf, size_t count, off_t offset)
133 rv = pwrite(fd, buf, count, offset);
136 } else if ( rv == -1 ) {
137 if ( errno == EINTR ) {
154 * Version of the read function suitable for libfat
156 int libfat_xpread(intptr_t pp, void *buf, size_t secsize, libfat_sector_t sector)
158 off_t offset = (off_t)sector * secsize + filesystem_offset;
159 return xpread(pp, buf, secsize, offset);
162 int main(int argc, char *argv[])
164 static unsigned char sectbuf[512];
166 const unsigned char *cdp;
173 char mntname[64], devfdname[64];
174 char *ldlinux_name, **argp, *opt;
175 int force = 0; /* -f (force) option */
176 struct libfat_filesystem *fs;
177 libfat_sector_t s, *secp, sectors[65]; /* 65 is maximum possible */
178 int32_t ldlinux_cluster;
182 (void)argc; /* Unused */
191 for ( argp = argv+1 ; *argp ; argp++ ) {
192 if ( **argp == '-' ) {
199 syslinux_make_stupid(); /* Use "safe, slow and stupid" code */
200 } else if ( *opt == 'f' ) {
201 force = 1; /* Force install */
202 } else if ( *opt == 'o' && argp[1] ) {
203 filesystem_offset = (off_t)strtoull(*++argp, NULL, 0); /* Byte offset */
220 * First make sure we can open the device at all, and that we have
221 * read/write permission.
223 dev_fd = open(device, O_RDWR);
224 if ( dev_fd < 0 || fstat(dev_fd, &st) < 0 ) {
229 if ( !force && !S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode) ) {
230 die("not a block device or regular file (use -f to override)");
233 if ( !force && filesystem_offset && !S_ISREG(st.st_mode) ) {
234 die("not a regular file and an offset specified (use -f to override)");
237 xpread(dev_fd, sectbuf, 512, filesystem_offset);
241 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
243 if( (errmsg = syslinux_check_bootsect(sectbuf)) ) {
244 fprintf(stderr, "%s: %s\n", device, errmsg);
249 * Now mount the device.
252 die("This program needs root privilege");
258 /* We're root or at least setuid.
259 Make a temp dir and pass all the gunky options to mount. */
261 if ( chdir("/tmp") ) {
266 #define TMP_MODE (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IWOTH|S_IXOTH|S_ISVTX)
268 if ( stat(".", &dst) || !S_ISDIR(dst.st_mode) ||
269 (dst.st_mode & TMP_MODE) != TMP_MODE ) {
270 die("possibly unsafe /tmp permissions");
273 for ( i = 0 ; ; i++ ) {
274 snprintf(mntname, sizeof mntname, "syslinux.mnt.%lu.%d",
275 (unsigned long)mypid, i);
277 if ( lstat(mntname, &dst) != -1 || errno != ENOENT )
280 rv = mkdir(mntname, 0000);
283 if ( errno == EEXIST || errno == EINTR )
289 if ( lstat(mntname, &dst) || dst.st_mode != (S_IFDIR|0000) ||
291 die("someone is trying to symlink race us!");
293 break; /* OK, got something... */
299 if ( S_ISREG(st.st_mode) ) {
300 /* It's file, need to mount it loopback */
302 struct loop_info64 loopinfo;
304 for ( n = 0 ; loop_fd < 0 ; n++ ) {
305 snprintf(devfdname, sizeof devfdname, "/dev/loop%u", n);
306 loop_fd = open(devfdname, O_RDWR);
307 if ( loop_fd < 0 && errno == ENOENT ) {
308 die("no available loopback device!");
310 if ( ioctl(loop_fd, LOOP_SET_FD, (void *)dev_fd) ) {
311 close(loop_fd); loop_fd = -1;
312 if ( errno != EBUSY )
313 die("cannot set up loopback device");
318 if ( ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo) ||
319 (loopinfo.lo_offset = filesystem_offset,
320 ioctl(loop_fd, LOOP_SET_STATUS64, &loopinfo)) )
321 die("cannot set up loopback device");
324 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
325 (unsigned long)mypid, dev_fd);
328 if ( mount(devfdname, mntpath, "msdos",
329 MS_NOEXEC|MS_NOSUID, "umask=077,quiet") )
330 die("could not mount filesystem");
334 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
335 (unsigned long)mypid, dev_fd);
342 } else if ( f == 0 ) {
344 if ( S_ISREG(st.st_mode) ) {
345 snprintf(mnt_opts, sizeof mnt_opts, "rw,nodev,noexec,loop,offset=%llu,umask=077,quiet",
346 (unsigned long long)filesystem_offset);
348 snprintf(mnt_opts, sizeof mnt_opts, "rw,nodev,noexec,umask=077,quiet");
350 execl(_PATH_MOUNT, _PATH_MOUNT, "-t", "msdos", "-o", mnt_opts,\
351 devfdname, mntpath, NULL);
352 _exit(255); /* execl failed */
355 w = waitpid(f, &status, 0);
356 if ( w != f || status ) {
358 exit(1); /* Mount failed */
364 ldlinux_name = alloca(strlen(mntpath)+13);
365 if ( !ldlinux_name ) {
370 sprintf(ldlinux_name, "%s/ldlinux.sys", mntpath);
372 unlink(ldlinux_name);
373 fd = open(ldlinux_name, O_WRONLY|O_CREAT|O_TRUNC, 0444);
380 cdp = syslinux_ldlinux;
381 left = syslinux_ldlinux_len;
383 nb = write(fd, cdp, left);
384 if ( nb == -1 && errno == EINTR )
386 else if ( nb <= 0 ) {
397 * I don't understand why I need this. Does the DOS filesystems
398 * not honour the mode passed to open()?
409 if ( umount2(mntpath, 0) )
410 die("could not umount path");
412 if ( loop_fd != -1 ) {
413 ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
424 } else if ( f == 0 ) {
425 execl(_PATH_UMOUNT, _PATH_UMOUNT, mntpath, NULL);
428 w = waitpid(f, &status, 0);
429 if ( w != f || status ) {
442 * Now, use libfat to create a block map. This probably
443 * should be changed to use ioctl(...,FIBMAP,...) since
444 * this is supposed to be a simple, privileged version
447 fs = libfat_open(libfat_xpread, dev_fd);
448 ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL);
451 s = libfat_clustertosector(fs, ldlinux_cluster);
452 while ( s && nsectors < 65 ) {
455 s = libfat_nextsector(fs, s);
460 * Patch ldlinux.sys and the boot sector
462 syslinux_patch(sectors, nsectors);
465 * Write the now-patched first sector of ldlinux.sys
467 xpwrite(dev_fd, syslinux_ldlinux, 512, filesystem_offset + ((off_t)sectors[0] << 9));
470 * To finish up, write the boot sector
473 /* Read the superblock again since it might have changed while mounted */
474 xpread(dev_fd, sectbuf, 512, filesystem_offset);
476 /* Copy the syslinux code into the boot sector */
477 syslinux_make_bootsect(sectbuf);
479 /* Write new boot sector */
480 xpwrite(dev_fd, sectbuf, 512, filesystem_offset);