version: Update to 4.08, update year to 2014
[syslinux/sherbszt.git] / linux / syslinux.c
blob4b13b7fe2b3c97285f1163e9696a656757ee17de
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1998-2008 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009-2010 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
17 * This is Linux-specific by now.
19 * This is an alternate version of the installer which doesn't require
20 * mtools, but requires root privilege.
24 * If DO_DIRECT_MOUNT is 0, call mount(8)
25 * If DO_DIRECT_MOUNT is 1, call mount(2)
27 #ifdef __KLIBC__
28 # define DO_DIRECT_MOUNT 1
29 #else
30 # define DO_DIRECT_MOUNT 0 /* glibc has broken losetup ioctls */
31 #endif
33 #define _GNU_SOURCE
34 #define _XOPEN_SOURCE 500 /* For pread() pwrite() */
35 #define _FILE_OFFSET_BITS 64
36 #include <alloca.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <paths.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <inttypes.h>
45 #include <sys/stat.h>
46 #include <sys/types.h>
47 #include <sys/wait.h>
48 #include <sys/mount.h>
50 #include "linuxioctl.h"
52 #include <paths.h>
53 #ifndef _PATH_MOUNT
54 # define _PATH_MOUNT "/bin/mount"
55 #endif
56 #ifndef _PATH_UMOUNT
57 # define _PATH_UMOUNT "/bin/umount"
58 #endif
59 #ifndef _PATH_TMP
60 # define _PATH_TMP "/tmp/"
61 #endif
63 #include "syslinux.h"
65 #if DO_DIRECT_MOUNT
66 # include <linux/loop.h>
67 #endif
69 #include <getopt.h>
70 #include <sysexits.h>
71 #include "syslxcom.h"
72 #include "syslxfs.h"
73 #include "setadv.h"
74 #include "syslxopt.h" /* unified options */
76 extern const char *program; /* Name of program */
78 pid_t mypid;
79 char *mntpath = NULL; /* Path on which to mount */
81 #if DO_DIRECT_MOUNT
82 int loop_fd = -1; /* Loop device */
83 #endif
85 void __attribute__ ((noreturn)) die(const char *msg)
87 fprintf(stderr, "%s: %s\n", program, msg);
89 #if DO_DIRECT_MOUNT
90 if (loop_fd != -1) {
91 ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
92 close(loop_fd);
93 loop_fd = -1;
95 #endif
97 if (mntpath)
98 unlink(mntpath);
100 exit(1);
104 * Mount routine
106 int do_mount(int dev_fd, int *cookie, const char *mntpath, const char *fstype)
108 struct stat st;
110 (void)cookie;
112 if (fstat(dev_fd, &st) < 0)
113 return errno;
115 #if DO_DIRECT_MOUNT
117 if (!S_ISBLK(st.st_mode)) {
118 /* It's file, need to mount it loopback */
119 unsigned int n = 0;
120 struct loop_info64 loopinfo;
121 int loop_fd;
123 for (n = 0; loop_fd < 0; n++) {
124 snprintf(devfdname, sizeof devfdname, "/dev/loop%u", n);
125 loop_fd = open(devfdname, O_RDWR);
126 if (loop_fd < 0 && errno == ENOENT) {
127 die("no available loopback device!");
129 if (ioctl(loop_fd, LOOP_SET_FD, (void *)dev_fd)) {
130 close(loop_fd);
131 loop_fd = -1;
132 if (errno != EBUSY)
133 die("cannot set up loopback device");
134 else
135 continue;
138 if (ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo) ||
139 (loopinfo.lo_offset = opt.offset,
140 ioctl(loop_fd, LOOP_SET_STATUS64, &loopinfo)))
141 die("cannot set up loopback device");
144 *cookie = loop_fd;
145 } else {
146 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
147 (unsigned long)mypid, dev_fd);
148 *cookie = -1;
151 return mount(devfdname, mntpath, fstype,
152 MS_NOEXEC | MS_NOSUID, "umask=077,quiet");
154 #else
156 char devfdname[128], mnt_opts[128];
157 pid_t f, w;
158 int status;
160 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
161 (unsigned long)mypid, dev_fd);
163 f = fork();
164 if (f < 0) {
165 return -1;
166 } else if (f == 0) {
167 if (!S_ISBLK(st.st_mode)) {
168 snprintf(mnt_opts, sizeof mnt_opts,
169 "rw,nodev,noexec,loop,offset=%llu,umask=077,quiet",
170 (unsigned long long)opt.offset);
171 } else {
172 snprintf(mnt_opts, sizeof mnt_opts,
173 "rw,nodev,noexec,umask=077,quiet");
175 execl(_PATH_MOUNT, _PATH_MOUNT, "-t", fstype, "-o", mnt_opts,
176 devfdname, mntpath, NULL);
177 _exit(255); /* execl failed */
180 w = waitpid(f, &status, 0);
181 return (w != f || status) ? -1 : 0;
183 #endif
187 * umount routine
189 void do_umount(const char *mntpath, int cookie)
191 #if DO_DIRECT_MOUNT
192 int loop_fd = cookie;
194 if (umount2(mntpath, 0))
195 die("could not umount path");
197 if (loop_fd != -1) {
198 ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
199 close(loop_fd);
200 loop_fd = -1;
202 #else
203 pid_t f = fork();
204 pid_t w;
205 int status;
206 (void)cookie;
208 if (f < 0) {
209 perror("fork");
210 exit(1);
211 } else if (f == 0) {
212 execl(_PATH_UMOUNT, _PATH_UMOUNT, mntpath, NULL);
215 w = waitpid(f, &status, 0);
216 if (w != f || status) {
217 exit(1);
219 #endif
223 * Modify the ADV of an existing installation
225 int modify_existing_adv(const char *path)
227 if (opt.reset_adv)
228 syslinux_reset_adv(syslinux_adv);
229 else if (read_adv(path, "ldlinux.sys") < 0)
230 return 1;
232 if (modify_adv() < 0)
233 return 1;
235 if (write_adv(path, "ldlinux.sys") < 0)
236 return 1;
238 return 0;
241 int main(int argc, char *argv[])
243 static unsigned char sectbuf[SECTOR_SIZE];
244 int dev_fd, fd;
245 struct stat st;
246 int err = 0;
247 char mntname[128];
248 char *ldlinux_name;
249 char *ldlinux_path;
250 char *subdir;
251 sector_t *sectors = NULL;
252 int ldlinux_sectors = (boot_image_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
253 const char *errmsg;
254 int mnt_cookie;
255 int patch_sectors;
256 int i;
258 mypid = getpid();
259 umask(077);
260 parse_options(argc, argv, MODE_SYSLINUX);
262 /* Note: subdir is guaranteed to start and end in / */
263 if (opt.directory && opt.directory[0]) {
264 int len = strlen(opt.directory);
265 int rv = asprintf(&subdir, "%s%s%s",
266 opt.directory[0] == '/' ? "" : "/",
267 opt.directory,
268 opt.directory[len-1] == '/' ? "" : "/");
269 if (rv < 0 || !subdir) {
270 perror(program);
271 exit(1);
273 } else {
274 subdir = "/";
277 if (!opt.device || opt.install_mbr || opt.activate_partition)
278 usage(EX_USAGE, MODE_SYSLINUX);
281 * First make sure we can open the device at all, and that we have
282 * read/write permission.
284 dev_fd = open(opt.device, O_RDWR);
285 if (dev_fd < 0 || fstat(dev_fd, &st) < 0) {
286 perror(opt.device);
287 exit(1);
290 if (!S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) {
291 die("not a device or regular file");
294 if (opt.offset && S_ISBLK(st.st_mode)) {
295 die("can't combine an offset with a block device");
298 xpread(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
299 fsync(dev_fd);
302 * Check to see that what we got was indeed an FAT/NTFS
303 * boot sector/superblock
305 if ((errmsg = syslinux_check_bootsect(sectbuf, &fs_type))) {
306 fprintf(stderr, "%s: %s\n", opt.device, errmsg);
307 exit(1);
311 * Now mount the device.
313 if (geteuid()) {
314 die("This program needs root privilege");
315 } else {
316 int i = 0;
317 struct stat dst;
318 int rv;
320 /* We're root or at least setuid.
321 Make a temp dir and pass all the gunky options to mount. */
323 if (chdir(_PATH_TMP)) {
324 fprintf(stderr, "%s: Cannot access the %s directory.\n",
325 program, _PATH_TMP);
326 exit(1);
328 #define TMP_MODE (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IWOTH|S_IXOTH|S_ISVTX)
330 if (stat(".", &dst) || !S_ISDIR(dst.st_mode) ||
331 (dst.st_mode & TMP_MODE) != TMP_MODE) {
332 die("possibly unsafe " _PATH_TMP " permissions");
335 for (i = 0;; i++) {
336 snprintf(mntname, sizeof mntname, "syslinux.mnt.%lu.%d",
337 (unsigned long)mypid, i);
339 if (lstat(mntname, &dst) != -1 || errno != ENOENT)
340 continue;
342 rv = mkdir(mntname, 0000);
344 if (rv == -1) {
345 if (errno == EEXIST || errno == EINTR)
346 continue;
347 perror(program);
348 exit(1);
351 if (lstat(mntname, &dst) || dst.st_mode != (S_IFDIR | 0000) ||
352 dst.st_uid != 0) {
353 die("someone is trying to symlink race us!");
355 break; /* OK, got something... */
358 mntpath = mntname;
361 if (fs_type == VFAT) {
362 if (do_mount(dev_fd, &mnt_cookie, mntpath, "vfat") &&
363 do_mount(dev_fd, &mnt_cookie, mntpath, "msdos")) {
364 rmdir(mntpath);
365 die("failed on mounting fat volume");
367 } else if (fs_type == NTFS) {
368 if (do_mount(dev_fd, &mnt_cookie, mntpath, "ntfs-3g")) {
369 rmdir(mntpath);
370 die("failed on mounting ntfs volume");
374 ldlinux_path = alloca(strlen(mntpath) + strlen(subdir) + 1);
375 sprintf(ldlinux_path, "%s%s", mntpath, subdir);
377 ldlinux_name = alloca(strlen(ldlinux_path) + 14);
378 if (!ldlinux_name) {
379 perror(program);
380 err = 1;
381 goto umount;
383 sprintf(ldlinux_name, "%sldlinux.sys", ldlinux_path);
385 /* update ADV only ? */
386 if (opt.update_only == -1) {
387 if (opt.reset_adv || opt.set_once) {
388 modify_existing_adv(ldlinux_path);
389 do_umount(mntpath, mnt_cookie);
390 sync();
391 rmdir(mntpath);
392 exit(0);
393 } else if (opt.update_only && !syslinux_already_installed(dev_fd)) {
394 fprintf(stderr, "%s: no previous syslinux boot sector found\n",
395 argv[0]);
396 exit(1);
397 } else {
398 fprintf(stderr, "%s: please specify --install or --update for the future\n", argv[0]);
399 opt.update_only = 0;
403 /* Read a pre-existing ADV, if already installed */
404 if (opt.reset_adv)
405 syslinux_reset_adv(syslinux_adv);
406 else if (read_adv(ldlinux_path, "ldlinux.sys") < 0)
407 syslinux_reset_adv(syslinux_adv);
408 if (modify_adv() < 0)
409 exit(1);
411 if ((fd = open(ldlinux_name, O_RDONLY)) >= 0) {
412 uint32_t zero_attr = 0;
413 ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &zero_attr);
414 close(fd);
417 unlink(ldlinux_name);
418 fd = open(ldlinux_name, O_WRONLY | O_CREAT | O_TRUNC, 0444);
419 if (fd < 0) {
420 perror(opt.device);
421 err = 1;
422 goto umount;
425 /* Write it the first time */
426 if (xpwrite(fd, boot_image, boot_image_len, 0) != (int)boot_image_len ||
427 xpwrite(fd, syslinux_adv, 2 * ADV_SIZE,
428 boot_image_len) != 2 * ADV_SIZE) {
429 fprintf(stderr, "%s: write failure on %s\n", program, ldlinux_name);
430 exit(1);
433 fsync(fd);
435 * Set the attributes
438 uint32_t attr = 0x07; /* Hidden+System+Readonly */
439 ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
443 * Create a block map.
445 ldlinux_sectors += 2; /* 2 ADV sectors */
446 sectors = calloc(ldlinux_sectors, sizeof *sectors);
447 if (sectmap(fd, sectors, ldlinux_sectors)) {
448 perror("bmap");
449 exit(1);
451 close(fd);
452 sync();
454 umount:
455 do_umount(mntpath, mnt_cookie);
456 sync();
457 rmdir(mntpath);
459 if (err)
460 exit(err);
463 * Patch ldlinux.sys and the boot sector
465 i = syslinux_patch(sectors, ldlinux_sectors, opt.stupid_mode,
466 opt.raid_mode, subdir, NULL);
467 patch_sectors = (i + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
470 * Write the now-patched first sectors of ldlinux.sys
472 for (i = 0; i < patch_sectors; i++) {
473 xpwrite(dev_fd, boot_image + i * SECTOR_SIZE, SECTOR_SIZE,
474 opt.offset + ((off_t) sectors[i] << SECTOR_SHIFT));
478 * To finish up, write the boot sector
481 /* Read the superblock again since it might have changed while mounted */
482 xpread(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
484 /* Copy the syslinux code into the boot sector */
485 syslinux_make_bootsect(sectbuf, fs_type);
487 /* Write new boot sector */
488 xpwrite(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
490 close(dev_fd);
491 sync();
493 /* Done! */
495 return 0;