Adding debian version 3.35-1.
[syslinux-debian/hramrach.git] / unix / syslinux.c
blob36826a1b499de033e08ed26c71e5a11bb18de7b0
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1998-2007 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
16 * This program ought to be portable. I hope so, at least.
18 * This is an alternate version of the installer which doesn't require
19 * mtools, but requires root privilege.
23 * If DO_DIRECT_MOUNT is 0, call mount(8)
24 * If DO_DIRECT_MOUNT is 1, call mount(2)
26 #ifdef __KLIBC__
27 # define DO_DIRECT_MOUNT 1
28 #else
29 # define DO_DIRECT_MOUNT 0 /* glibc has broken losetup ioctls */
30 #endif
32 #define _GNU_SOURCE
33 #define _XOPEN_SOURCE 500 /* For pread() pwrite() */
34 #define _FILE_OFFSET_BITS 64
35 #include <alloca.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <paths.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <inttypes.h>
44 #include <sys/stat.h>
45 #include <sys/types.h>
46 #include <sys/wait.h>
47 #include <sys/mount.h>
49 #include "syslinux.h"
50 #include "libfat.h"
52 #if DO_DIRECT_MOUNT
54 # include <linux/loop.h>
56 #else
58 # include <paths.h>
59 # ifndef _PATH_MOUNT
60 # define _PATH_MOUNT "/bin/mount"
61 # endif
62 # ifndef _PATH_UMOUNT
63 # define _PATH_UMOUNT "/bin/umount"
64 # endif
66 #endif
68 const char *program; /* Name of program */
69 const char *device; /* Device to install to */
70 pid_t mypid;
71 char *mntpath = NULL; /* Path on which to mount */
72 off_t filesystem_offset = 0; /* Filesystem offset */
73 #if DO_DIRECT_MOUNT
74 int loop_fd = -1; /* Loop device */
75 #endif
77 void __attribute__((noreturn)) usage(void)
79 fprintf(stderr, "Usage: %s [-sf][-d directory][-o offset] device\n", program);
80 exit(1);
83 void __attribute__((noreturn)) die(const char *msg)
85 fprintf(stderr, "%s: %s\n", program, msg);
87 #if DO_DIRECT_MOUNT
88 if ( loop_fd != -1 ) {
89 ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
90 close(loop_fd);
91 loop_fd = -1;
93 #endif
95 if ( mntpath )
96 unlink(mntpath);
98 exit(1);
102 * read/write wrapper functions
104 ssize_t xpread(int fd, void *buf, size_t count, off_t offset)
106 char *bufp = (char *)buf;
107 ssize_t rv;
108 ssize_t done = 0;
110 while ( count ) {
111 rv = pread(fd, bufp, count, offset);
112 if ( rv == 0 ) {
113 die("short read");
114 } else if ( rv == -1 ) {
115 if ( errno == EINTR ) {
116 continue;
117 } else {
118 die(strerror(errno));
120 } else {
121 bufp += rv;
122 offset += rv;
123 done += rv;
124 count -= rv;
128 return done;
131 ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset)
133 const char *bufp = (const char *)buf;
134 ssize_t rv;
135 ssize_t done = 0;
137 while ( count ) {
138 rv = pwrite(fd, bufp, count, offset);
139 if ( rv == 0 ) {
140 die("short write");
141 } else if ( rv == -1 ) {
142 if ( errno == EINTR ) {
143 continue;
144 } else {
145 die(strerror(errno));
147 } else {
148 bufp += rv;
149 offset += rv;
150 done += rv;
151 count -= rv;
155 return done;
159 * Version of the read function suitable for libfat
161 int libfat_xpread(intptr_t pp, void *buf, size_t secsize, libfat_sector_t sector)
163 off_t offset = (off_t)sector * secsize + filesystem_offset;
164 return xpread(pp, buf, secsize, offset);
167 int main(int argc, char *argv[])
169 static unsigned char sectbuf[512];
170 unsigned char *dp;
171 const unsigned char *cdp;
172 int dev_fd, fd;
173 struct stat st;
174 int nb, left;
175 int err = 0;
176 pid_t f, w;
177 int status;
178 char mntname[64], devfdname[64];
179 char *ldlinux_name, **argp, *opt;
180 int force = 0; /* -f (force) option */
181 const char *subdir = NULL;
182 struct libfat_filesystem *fs;
183 struct libfat_direntry dentry;
184 libfat_sector_t s, *secp, sectors[65]; /* 65 is maximum possible */
185 int32_t ldlinux_cluster;
186 int nsectors = 0;
187 const char *errmsg;
189 (void)argc; /* Unused */
191 program = argv[0];
192 mypid = getpid();
194 device = NULL;
196 umask(077);
198 for ( argp = argv+1 ; *argp ; argp++ ) {
199 if ( **argp == '-' ) {
200 opt = *argp + 1;
201 if ( !*opt )
202 usage();
204 while ( *opt ) {
205 if ( *opt == 's' ) {
206 syslinux_make_stupid(); /* Use "safe, slow and stupid" code */
207 } else if ( *opt == 'f' ) {
208 force = 1; /* Force install */
209 } else if ( *opt == 'd' && argp[1] ) {
210 subdir = *++argp;
211 } else if ( *opt == 'o' && argp[1] ) {
212 filesystem_offset = (off_t)strtoull(*++argp, NULL, 0); /* Byte offset */
213 } else {
214 usage();
216 opt++;
218 } else {
219 if ( device )
220 usage();
221 device = *argp;
225 if ( !device )
226 usage();
229 * First make sure we can open the device at all, and that we have
230 * read/write permission.
232 dev_fd = open(device, O_RDWR);
233 if ( dev_fd < 0 || fstat(dev_fd, &st) < 0 ) {
234 perror(device);
235 exit(1);
238 if ( !force && !S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode) ) {
239 die("not a block device or regular file (use -f to override)");
242 if ( !force && filesystem_offset && !S_ISREG(st.st_mode) ) {
243 die("not a regular file and an offset specified (use -f to override)");
246 xpread(dev_fd, sectbuf, 512, filesystem_offset);
247 fsync(dev_fd);
250 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
252 if( (errmsg = syslinux_check_bootsect(sectbuf)) ) {
253 fprintf(stderr, "%s: %s\n", device, errmsg);
254 exit(1);
258 * Now mount the device.
260 if ( geteuid() ) {
261 die("This program needs root privilege");
262 } else {
263 int i = 0;
264 struct stat dst;
265 int rv;
267 /* We're root or at least setuid.
268 Make a temp dir and pass all the gunky options to mount. */
270 if ( chdir("/tmp") ) {
271 perror(program);
272 exit(1);
275 #define TMP_MODE (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IWOTH|S_IXOTH|S_ISVTX)
277 if ( stat(".", &dst) || !S_ISDIR(dst.st_mode) ||
278 (dst.st_mode & TMP_MODE) != TMP_MODE ) {
279 die("possibly unsafe /tmp permissions");
282 for ( i = 0 ; ; i++ ) {
283 snprintf(mntname, sizeof mntname, "syslinux.mnt.%lu.%d",
284 (unsigned long)mypid, i);
286 if ( lstat(mntname, &dst) != -1 || errno != ENOENT )
287 continue;
289 rv = mkdir(mntname, 0000);
291 if ( rv == -1 ) {
292 if ( errno == EEXIST || errno == EINTR )
293 continue;
294 perror(program);
295 exit(1);
298 if ( lstat(mntname, &dst) || dst.st_mode != (S_IFDIR|0000) ||
299 dst.st_uid != 0 ) {
300 die("someone is trying to symlink race us!");
302 break; /* OK, got something... */
305 mntpath = mntname;
307 #if DO_DIRECT_MOUNT
308 if ( S_ISREG(st.st_mode) ) {
309 /* It's file, need to mount it loopback */
310 unsigned int n = 0;
311 struct loop_info64 loopinfo;
313 for ( n = 0 ; loop_fd < 0 ; n++ ) {
314 snprintf(devfdname, sizeof devfdname, "/dev/loop%u", n);
315 loop_fd = open(devfdname, O_RDWR);
316 if ( loop_fd < 0 && errno == ENOENT ) {
317 die("no available loopback device!");
319 if ( ioctl(loop_fd, LOOP_SET_FD, (void *)dev_fd) ) {
320 close(loop_fd); loop_fd = -1;
321 if ( errno != EBUSY )
322 die("cannot set up loopback device");
323 else
324 continue;
327 if ( ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo) ||
328 (loopinfo.lo_offset = filesystem_offset,
329 ioctl(loop_fd, LOOP_SET_STATUS64, &loopinfo)) )
330 die("cannot set up loopback device");
332 } else {
333 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
334 (unsigned long)mypid, dev_fd);
337 if ( mount(devfdname, mntpath, "msdos",
338 MS_NOEXEC|MS_NOSUID, "umask=077,quiet") )
339 die("could not mount filesystem");
341 #else
343 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
344 (unsigned long)mypid, dev_fd);
346 f = fork();
347 if ( f < 0 ) {
348 perror(program);
349 rmdir(mntpath);
350 exit(1);
351 } else if ( f == 0 ) {
352 char mnt_opts[128];
353 if ( S_ISREG(st.st_mode) ) {
354 snprintf(mnt_opts, sizeof mnt_opts, "rw,nodev,noexec,loop,offset=%llu,umask=077,quiet",
355 (unsigned long long)filesystem_offset);
356 } else {
357 snprintf(mnt_opts, sizeof mnt_opts, "rw,nodev,noexec,umask=077,quiet");
359 execl(_PATH_MOUNT, _PATH_MOUNT, "-t", "msdos", "-o", mnt_opts,\
360 devfdname, mntpath, NULL);
361 _exit(255); /* execl failed */
364 w = waitpid(f, &status, 0);
365 if ( w != f || status ) {
366 rmdir(mntpath);
367 exit(1); /* Mount failed */
370 #endif
373 ldlinux_name = alloca(strlen(mntpath)+14);
374 if ( !ldlinux_name ) {
375 perror(program);
376 err = 1;
377 goto umount;
379 sprintf(ldlinux_name, "%s//ldlinux.sys", mntpath);
381 unlink(ldlinux_name);
382 fd = open(ldlinux_name, O_WRONLY|O_CREAT|O_TRUNC, 0444);
383 if ( fd < 0 ) {
384 perror(device);
385 err = 1;
386 goto umount;
389 cdp = syslinux_ldlinux;
390 left = syslinux_ldlinux_len;
391 while ( left ) {
392 nb = write(fd, cdp, left);
393 if ( nb == -1 && errno == EINTR )
394 continue;
395 else if ( nb <= 0 ) {
396 perror(device);
397 err = 1;
398 goto umount;
401 dp += nb;
402 left -= nb;
406 * I don't understand why I need this. Does the DOS filesystems
407 * not honour the mode passed to open()?
409 fchmod(fd, 0400);
411 close(fd);
413 sync();
416 * Now, use libfat to create a block map. This probably
417 * should be changed to use ioctl(...,FIBMAP,...) since
418 * this is supposed to be a simple, privileged version
419 * of the installer.
421 fs = libfat_open(libfat_xpread, dev_fd);
422 ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", &dentry);
423 secp = sectors;
424 nsectors = 0;
425 s = libfat_clustertosector(fs, ldlinux_cluster);
426 while ( s && nsectors < 65 ) {
427 *secp++ = s;
428 nsectors++;
429 s = libfat_nextsector(fs, s);
431 libfat_close(fs);
433 /* Move ldlinux.sys to the desired location */
434 if (subdir) {
435 char *new_ldlinux_name = alloca(strlen(mntpath)+
436 strlen(subdir)+15);
437 int mov_err = 1;
439 if ( new_ldlinux_name ) {
440 sprintf(new_ldlinux_name, "%s//%s//ldlinux.sys", mntpath, subdir);
442 if (!rename(ldlinux_name, new_ldlinux_name))
443 mov_err = 0;
446 if (mov_err)
447 fprintf(stderr, "%s: warning: unable to move ldlinux.sys: %s\n",
448 device, strerror(errno));
451 umount:
452 #if DO_DIRECT_MOUNT
454 if ( umount2(mntpath, 0) )
455 die("could not umount path");
457 if ( loop_fd != -1 ) {
458 ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
459 close(loop_fd);
460 loop_fd = -1;
463 #else
465 f = fork();
466 if ( f < 0 ) {
467 perror("fork");
468 exit(1);
469 } else if ( f == 0 ) {
470 execl(_PATH_UMOUNT, _PATH_UMOUNT, mntpath, NULL);
473 w = waitpid(f, &status, 0);
474 if ( w != f || status ) {
475 exit(1);
478 #endif
480 sync();
481 rmdir(mntpath);
483 if ( err )
484 exit(err);
487 * Patch ldlinux.sys and the boot sector
489 syslinux_patch(sectors, nsectors);
492 * Write the now-patched first sector of ldlinux.sys
494 xpwrite(dev_fd, syslinux_ldlinux, 512, filesystem_offset + ((off_t)sectors[0] << 9));
497 * Patch the root directory to set attributes to
498 * HIDDEN|SYSTEM|READONLY
501 const unsigned char attrib = 0x07;
502 xpwrite(dev_fd, &attrib, 1, ((off_t)dentry.sector << 9)+dentry.offset+11);
506 * To finish up, write the boot sector
509 /* Read the superblock again since it might have changed while mounted */
510 xpread(dev_fd, sectbuf, 512, filesystem_offset);
512 /* Copy the syslinux code into the boot sector */
513 syslinux_make_bootsect(sectbuf);
515 /* Write new boot sector */
516 xpwrite(dev_fd, sectbuf, 512, filesystem_offset);
518 close(dev_fd);
519 sync();
521 /* Done! */
523 return 0;