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