Merge syslinux/extlinux patch code and core code
[profile/ivi/syslinux.git] / linux / syslinux.c
1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 1998-2008 H. Peter Anvin - All Rights Reserved
4  *   Copyright 2009-2010 Intel Corporation; author: H. Peter Anvin
5  *
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.
11  *
12  * ----------------------------------------------------------------------- */
13
14 /*
15  * syslinux.c - Linux installer program for SYSLINUX
16  *
17  * This is Linux-specific by now.
18  *
19  * This is an alternate version of the installer which doesn't require
20  * mtools, but requires root privilege.
21  */
22
23 /*
24  * If DO_DIRECT_MOUNT is 0, call mount(8)
25  * If DO_DIRECT_MOUNT is 1, call mount(2)
26  */
27 #ifdef __KLIBC__
28 # define DO_DIRECT_MOUNT 1
29 #else
30 # define DO_DIRECT_MOUNT 0      /* glibc has broken losetup ioctls */
31 #endif
32
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>
49
50 #include "linuxioctl.h"
51
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
62
63 #include "syslinux.h"
64
65 #if DO_DIRECT_MOUNT
66 # include <linux/loop.h>
67 #endif
68
69 #include <getopt.h>
70 #include <sysexits.h>
71 #include "syslxcom.h"
72 #include "setadv.h"
73 #include "syslxopt.h" /* unified options */
74
75 extern const char *program;     /* Name of program */
76
77 pid_t mypid;
78 char *mntpath = NULL;           /* Path on which to mount */
79
80 /*
81  * Image file
82  */
83 #define boot_image      syslinux_ldlinux
84 #define boot_image_len  syslinux_ldlinux_len
85
86 #if DO_DIRECT_MOUNT
87 int loop_fd = -1;               /* Loop device */
88 #endif
89
90 void __attribute__ ((noreturn)) die(const char *msg)
91 {
92     fprintf(stderr, "%s: %s\n", program, msg);
93
94 #if DO_DIRECT_MOUNT
95     if (loop_fd != -1) {
96         ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
97         close(loop_fd);
98         loop_fd = -1;
99     }
100 #endif
101
102     if (mntpath)
103         unlink(mntpath);
104
105     exit(1);
106 }
107
108 /*
109  * Mount routine
110  */
111 int do_mount(int dev_fd, int *cookie, const char *mntpath, const char *fstype)
112 {
113     struct stat st;
114
115     (void)cookie;
116
117     if (fstat(dev_fd, &st) < 0)
118         return errno;
119
120 #if DO_DIRECT_MOUNT
121     {
122         if (!S_ISBLK(st.st_mode)) {
123             /* It's file, need to mount it loopback */
124             unsigned int n = 0;
125             struct loop_info64 loopinfo;
126             int loop_fd;
127
128             for (n = 0; loop_fd < 0; n++) {
129                 snprintf(devfdname, sizeof devfdname, "/dev/loop%u", n);
130                 loop_fd = open(devfdname, O_RDWR);
131                 if (loop_fd < 0 && errno == ENOENT) {
132                     die("no available loopback device!");
133                 }
134                 if (ioctl(loop_fd, LOOP_SET_FD, (void *)dev_fd)) {
135                     close(loop_fd);
136                     loop_fd = -1;
137                     if (errno != EBUSY)
138                         die("cannot set up loopback device");
139                     else
140                         continue;
141                 }
142
143                 if (ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo) ||
144                     (loopinfo.lo_offset = opt.offset,
145                      ioctl(loop_fd, LOOP_SET_STATUS64, &loopinfo)))
146                     die("cannot set up loopback device");
147             }
148
149             *cookie = loop_fd;
150         } else {
151             snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
152                      (unsigned long)mypid, dev_fd);
153             *cookie = -1;
154         }
155
156         return mount(devfdname, mntpath, fstype,
157                      MS_NOEXEC | MS_NOSUID, "umask=077,quiet");
158     }
159 #else
160     {
161         char devfdname[128], mnt_opts[128];
162         pid_t f, w;
163         int status;
164
165         snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
166                  (unsigned long)mypid, dev_fd);
167
168         f = fork();
169         if (f < 0) {
170             return -1;
171         } else if (f == 0) {
172             if (!S_ISBLK(st.st_mode)) {
173                 snprintf(mnt_opts, sizeof mnt_opts,
174                          "rw,nodev,noexec,loop,offset=%llu,umask=077,quiet",
175                          (unsigned long long)opt.offset);
176             } else {
177                 snprintf(mnt_opts, sizeof mnt_opts,
178                          "rw,nodev,noexec,umask=077,quiet");
179             }
180             execl(_PATH_MOUNT, _PATH_MOUNT, "-t", fstype, "-o", mnt_opts,
181                   devfdname, mntpath, NULL);
182             _exit(255);         /* execl failed */
183         }
184
185         w = waitpid(f, &status, 0);
186         return (w != f || status) ? -1 : 0;
187     }
188 #endif
189 }
190
191 /*
192  * umount routine
193  */
194 void do_umount(const char *mntpath, int cookie)
195 {
196 #if DO_DIRECT_MOUNT
197     int loop_fd = cookie;
198
199     if (umount2(mntpath, 0))
200         die("could not umount path");
201
202     if (loop_fd != -1) {
203         ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
204         close(loop_fd);
205         loop_fd = -1;
206     }
207 #else
208     pid_t f = fork();
209     pid_t w;
210     int status;
211     (void)cookie;
212
213     if (f < 0) {
214         perror("fork");
215         exit(1);
216     } else if (f == 0) {
217         execl(_PATH_UMOUNT, _PATH_UMOUNT, mntpath, NULL);
218     }
219
220     w = waitpid(f, &status, 0);
221     if (w != f || status) {
222         exit(1);
223     }
224 #endif
225 }
226
227 /*
228  * Make any user-specified ADV modifications
229  */
230 int modify_adv(void)
231 {
232     int rv = 0;
233
234     if (opt.set_once) {
235         if (syslinux_setadv(ADV_BOOTONCE, strlen(opt.set_once), opt.set_once)) {
236             fprintf(stderr, "%s: not enough space for boot-once command\n",
237                     program);
238             rv = -1;
239         }
240     }
241
242     return rv;
243 }
244
245 /*
246  * Modify the ADV of an existing installation
247  */
248 int modify_existing_adv(const char *path)
249 {
250     if (opt.reset_adv)
251         syslinux_reset_adv(syslinux_adv);
252     else if (read_adv(path, "ldlinux.sys") < 0)
253         return 1;
254
255     if (modify_adv() < 0)
256         return 1;
257
258     if (write_adv(path, "ldlinux.sys") < 0)
259         return 1;
260
261     return 0;
262 }
263
264 int main(int argc, char *argv[])
265 {
266     static unsigned char sectbuf[SECTOR_SIZE];
267     int dev_fd, fd;
268     struct stat st;
269     int err = 0;
270     char mntname[128];
271     char *ldlinux_name;
272     char *ldlinux_path;
273     char *subdir;
274     sector_t *sectors = NULL;
275     int ldlinux_sectors = (boot_image_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
276     const char *errmsg;
277     int mnt_cookie;
278     int patch_sectors;
279     int i;
280
281     mypid = getpid();
282     umask(077);
283     parse_options(argc, argv, MODE_SYSLINUX);
284
285     /* Note: subdir is guaranteed to start and end in / */
286     if (opt.directory && opt.directory[0]) {
287         int len = strlen(opt.directory);
288         asprintf(&subdir, "%s%s%s",
289                  opt.directory[0] == '/' ? "" : "/",
290                  opt.directory,
291                  opt.directory[len-1] == '/' ? "" : "/");
292         if (!subdir) {
293             perror(program);
294             exit(1);
295         }
296     } else {
297         subdir = "/";
298     }
299
300     if (!opt.device)
301         usage(EX_USAGE, MODE_SYSLINUX);
302
303     /*
304      * First make sure we can open the device at all, and that we have
305      * read/write permission.
306      */
307     dev_fd = open(opt.device, O_RDWR);
308     if (dev_fd < 0 || fstat(dev_fd, &st) < 0) {
309         perror(opt.device);
310         exit(1);
311     }
312
313     if (!S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) {
314         die("not a device or regular file");
315     }
316
317     if (opt.offset && S_ISBLK(st.st_mode)) {
318         die("can't combine an offset with a block device");
319     }
320
321     fs_type = VFAT;
322     xpread(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
323     fsync(dev_fd);
324
325     /*
326      * Check to see that what we got was indeed an MS-DOS boot sector/superblock
327      */
328     if ((errmsg = syslinux_check_bootsect(sectbuf))) {
329         fprintf(stderr, "%s: %s\n", opt.device, errmsg);
330         exit(1);
331     }
332
333     /*
334      * Now mount the device.
335      */
336     if (geteuid()) {
337         die("This program needs root privilege");
338     } else {
339         int i = 0;
340         struct stat dst;
341         int rv;
342
343         /* We're root or at least setuid.
344            Make a temp dir and pass all the gunky options to mount. */
345
346         if (chdir(_PATH_TMP)) {
347             perror(program);
348             exit(1);
349         }
350 #define TMP_MODE (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IWOTH|S_IXOTH|S_ISVTX)
351
352         if (stat(".", &dst) || !S_ISDIR(dst.st_mode) ||
353             (dst.st_mode & TMP_MODE) != TMP_MODE) {
354             die("possibly unsafe " _PATH_TMP " permissions");
355         }
356
357         for (i = 0;; i++) {
358             snprintf(mntname, sizeof mntname, "syslinux.mnt.%lu.%d",
359                      (unsigned long)mypid, i);
360
361             if (lstat(mntname, &dst) != -1 || errno != ENOENT)
362                 continue;
363
364             rv = mkdir(mntname, 0000);
365
366             if (rv == -1) {
367                 if (errno == EEXIST || errno == EINTR)
368                     continue;
369                 perror(program);
370                 exit(1);
371             }
372
373             if (lstat(mntname, &dst) || dst.st_mode != (S_IFDIR | 0000) ||
374                 dst.st_uid != 0) {
375                 die("someone is trying to symlink race us!");
376             }
377             break;              /* OK, got something... */
378         }
379
380         mntpath = mntname;
381     }
382
383     if (do_mount(dev_fd, &mnt_cookie, mntpath, "vfat") &&
384         do_mount(dev_fd, &mnt_cookie, mntpath, "msdos")) {
385         rmdir(mntpath);
386         die("mount failed");
387     }
388
389     ldlinux_path = alloca(strlen(mntpath) + strlen(subdir) + 1);
390     sprintf(ldlinux_path, "%s%s", mntpath, subdir);
391
392     ldlinux_name = alloca(strlen(ldlinux_path) + 14);
393     if (!ldlinux_name) {
394         perror(program);
395         err = 1;
396         goto umount;
397     }
398     sprintf(ldlinux_name, "%sldlinux.sys", ldlinux_path);
399
400     /* update ADV only ? */
401     if (opt.update_only == -1) {
402         if (opt.reset_adv || opt.set_once) {
403             modify_existing_adv(ldlinux_path);
404             do_umount(mntpath, mnt_cookie);
405             sync();
406             rmdir(mntpath);
407             exit(0);
408         } else {
409             fprintf(stderr, "%s: please specify --install or --update for the future\n", argv[0]);
410             opt.update_only = 0;
411         }
412     }
413
414     /* Read a pre-existing ADV, if already installed */
415     if (opt.reset_adv)
416         syslinux_reset_adv(syslinux_adv);
417     else if (read_adv(ldlinux_path, "ldlinux.sys") < 0)
418         syslinux_reset_adv(syslinux_adv);
419     if (modify_adv() < 0)
420         exit(1);
421
422     if ((fd = open(ldlinux_name, O_RDONLY)) >= 0) {
423         uint32_t zero_attr = 0;
424         ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &zero_attr);
425         close(fd);
426     }
427
428     unlink(ldlinux_name);
429     fd = open(ldlinux_name, O_WRONLY | O_CREAT | O_TRUNC, 0444);
430     if (fd < 0) {
431         perror(opt.device);
432         err = 1;
433         goto umount;
434     }
435
436     /* Write it the first time */
437     if (xpwrite(fd, boot_image, boot_image_len, 0) != (int)boot_image_len ||
438         xpwrite(fd, syslinux_adv, 2 * ADV_SIZE,
439                 boot_image_len) != 2 * ADV_SIZE) {
440         fprintf(stderr, "%s: write failure on %s\n", program, ldlinux_name);
441         exit(1);
442     }
443
444     fsync(fd);
445     /*
446      * Set the attributes
447      */
448     {
449         uint32_t attr = 0x07;   /* Hidden+System+Readonly */
450         ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
451     }
452
453     /*
454      * Create a block map.
455      */
456     ldlinux_sectors += 2; /* 2 ADV sectors */
457     sectors = calloc(ldlinux_sectors, sizeof *sectors);
458     if (sectmap(fd, sectors, ldlinux_sectors)) {
459         perror("bmap");
460         exit(1);
461     }
462     close(fd);
463     sync();
464
465 umount:
466     do_umount(mntpath, mnt_cookie);
467     sync();
468     rmdir(mntpath);
469
470     if (err)
471         exit(err);
472
473     /*
474      * Patch ldlinux.sys and the boot sector
475      */
476     i = syslinux_patch(sectors, ldlinux_sectors, opt.stupid_mode,
477                        opt.raid_mode, subdir, NULL);
478     patch_sectors = (i + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
479
480     /*
481      * Write the now-patched first sectors of ldlinux.sys
482      */
483     for (i = 0; i < patch_sectors; i++) {
484         xpwrite(dev_fd, boot_image + i * SECTOR_SIZE, SECTOR_SIZE,
485                 opt.offset + ((off_t) sectors[i] << SECTOR_SHIFT));
486     }
487
488     /*
489      * To finish up, write the boot sector
490      */
491
492     /* Read the superblock again since it might have changed while mounted */
493     xpread(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
494
495     /* Copy the syslinux code into the boot sector */
496     syslinux_make_bootsect(sectbuf);
497
498     /* Write new boot sector */
499     xpwrite(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
500
501     close(dev_fd);
502     sync();
503
504     /* Done! */
505
506     return 0;
507 }