📄 extlinux.c
字号:
#ident "$Id$"/* ----------------------------------------------------------------------- * * * Copyright 1998-2005 H. Peter Anvin - All Rights Reserved * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, Inc., 53 Temple Place Ste 330, * Boston MA 02111-1307, USA; either version 2 of the License, or * (at your option) any later version; incorporated herein by reference. * * ----------------------------------------------------------------------- *//* * extlinux.c * * Install the extlinux boot block on an ext2/3 filesystem */#define _GNU_SOURCE /* Enable everything */#include <inttypes.h>/* This is needed to deal with the kernel headers imported into glibc 3.3.3. */typedef uint64_t u64;#include <alloca.h>#include <errno.h>#include <fcntl.h>#include <stdio.h>#include <unistd.h>#include <mntent.h>#include <stdlib.h>#include <string.h>#include <getopt.h>#include <sysexits.h>#include <sys/ioctl.h>#include <sys/stat.h>#include <sys/types.h>#include <sys/mount.h>#include <linux/fd.h> /* Floppy geometry */#include <linux/hdreg.h> /* Hard disk geometry */#include <linux/fs.h> /* FIGETBSZ, FIBMAP */#include "ext2_fs.h"#include "../version.h"#ifdef DEBUG# define dprintf printf#else# define dprintf(...) ((void)0)#endif/* Global option handling */const char *program;/* These are the options we can set and their values */struct my_options { unsigned int sectors; unsigned int heads;} opt = { .sectors = 0, .heads = 0,};static void __attribute__((noreturn)) usage(int rv){ fprintf(stderr, "Usage: %s [options] directory\n" " --install -i Install over the current bootsector\n" " --update -U Update a previous EXTLINUX installation\n" " --zip -z Force zipdrive geometry (-H 64 -S 32)\n" " --sectors=# -S Force the number of sectors per track\n" " --heads=# -H Force number of heads\n" "\n" " Note: geometry is determined at boot time for devices which\n" " are considered hard disks by the BIOS. Unfortunately, this is\n" " not possible for devices which are considered floppy disks,\n" " which includes zipdisks and LS-120 superfloppies.\n" "\n" " The -z option is useful for USB devices which are considered\n" " hard disks by some BIOSes and zipdrives by other BIOSes.\n", program); exit(rv);}static const struct option long_options[] = { { "install", 0, NULL, 'i' }, { "update", 0, NULL, 'U' }, { "zipdrive", 0, NULL, 'z' }, { "sectors", 1, NULL, 'S' }, { "heads", 1, NULL, 'H' }, { "version", 0, NULL, 'v' }, { "help", 0, NULL, 'h' }, { 0, 0, 0, 0 }}; static const char short_options[] = "iUuzS:H:vh";#if defined(__linux__) && !defined(BLKGETSIZE64)/* This takes a u64, but the size field says size_t. Someone screwed big. */# define BLKGETSIZE64 _IOR(0x12,114,size_t)#endif#define LDLINUX_MAGIC 0x3eb202feenum bs_offsets { bsJump = 0x00, bsOemName = 0x03, bsBytesPerSec = 0x0b, bsSecPerClust = 0x0d, bsResSectors = 0x0e, bsFATs = 0x10, bsRootDirEnts = 0x11, bsSectors = 0x13, bsMedia = 0x15, bsFATsecs = 0x16, bsSecPerTrack = 0x18, bsHeads = 0x1a, bsHiddenSecs = 0x1c, bsHugeSectors = 0x20, /* FAT12/16 only */ bs16DriveNumber = 0x24, bs16Reserved1 = 0x25, bs16BootSignature = 0x26, bs16VolumeID = 0x27, bs16VolumeLabel = 0x2b, bs16FileSysType = 0x36, bs16Code = 0x3e, /* FAT32 only */ bs32FATSz32 = 36, bs32ExtFlags = 40, bs32FSVer = 42, bs32RootClus = 44, bs32FSInfo = 48, bs32BkBootSec = 50, bs32Reserved = 52, bs32DriveNumber = 64, bs32Reserved1 = 65, bs32BootSignature = 66, bs32VolumeID = 67, bs32VolumeLabel = 71, bs32FileSysType = 82, bs32Code = 90, bsSignature = 0x1fe};#define bsHead bsJump#define bsHeadLen (bsOemName-bsHead)#define bsCode bs32Code /* The common safe choice */#define bsCodeLen (bsSignature-bs32Code)/* * Access functions for littleendian numbers, possibly misaligned. */static inline uint8_t get_8(const unsigned char *p){ return *(const uint8_t *)p;}static inline uint16_t get_16(const unsigned char *p){#if defined(__i386__) || defined(__x86_64__) /* Littleendian and unaligned-capable */ return *(const uint16_t *)p;#else return (uint16_t)p[0] + ((uint16_t)p[1] << 8);#endif}static inline uint32_t get_32(const unsigned char *p){#if defined(__i386__) || defined(__x86_64__) /* Littleendian and unaligned-capable */ return *(const uint32_t *)p;#else return (uint32_t)p[0] + ((uint32_t)p[1] << 8) + ((uint32_t)p[2] << 16) + ((uint32_t)p[3] << 24);#endif}static inline void set_16(unsigned char *p, uint16_t v){#if defined(__i386__) || defined(__x86_64__) /* Littleendian and unaligned-capable */ *(uint16_t *)p = v;#else p[0] = (v & 0xff); p[1] = ((v >> 8) & 0xff);#endif}static inline void set_32(unsigned char *p, uint32_t v){#if defined(__i386__) || defined(__x86_64__) /* Littleendian and unaligned-capable */ *(uint32_t *)p = v;#else p[0] = (v & 0xff); p[1] = ((v >> 8) & 0xff); p[2] = ((v >> 16) & 0xff); p[3] = ((v >> 24) & 0xff);#endif}#ifndef EXT2_SUPER_OFFSET#define EXT2_SUPER_OFFSET 1024#endif#define SECTOR_SHIFT 9 /* 512-byte sectors */#define SECTOR_SIZE (1 << SECTOR_SHIFT)const char *program;/* * Boot block */extern unsigned char extlinux_bootsect[];extern unsigned int extlinux_bootsect_len;#define boot_block extlinux_bootsect#define boot_block_len extlinux_bootsect_len/* * Image file */extern unsigned char extlinux_image[];extern unsigned int extlinux_image_len;#define boot_image extlinux_image#define boot_image_len extlinux_image_len/* * Common abort function */void __attribute__((noreturn)) die(const char *msg){ fputs(msg, stderr); exit(1);}/* * read/write wrapper functions */ssize_t xpread(int fd, void *buf, size_t count, off_t offset){ char *bufp = (char *)buf; ssize_t rv; ssize_t done = 0; while ( count ) { rv = pread(fd, bufp, count, offset); if ( rv == 0 ) { die("short read"); } else if ( rv == -1 ) { if ( errno == EINTR ) { continue; } else { die(strerror(errno)); } } else { bufp += rv; offset += rv; done += rv; count -= rv; } } return done;}ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset){ const char *bufp = (const char *)buf; ssize_t rv; ssize_t done = 0; while ( count ) { rv = pwrite(fd, bufp, count, offset); if ( rv == 0 ) { die("short write"); } else if ( rv == -1 ) { if ( errno == EINTR ) { continue; } else { die(strerror(errno)); } } else { bufp += rv; offset += rv; done += rv; count -= rv; } } return done;}/* * Produce file map */intsectmap(int fd, uint32_t *sectors, int nsectors){ unsigned int blksize, blk, nblk; unsigned int i; /* Get block size */ if ( ioctl(fd, FIGETBSZ, &blksize) ) return -1; /* Number of sectors per block */ blksize >>= SECTOR_SHIFT; nblk = 0; while ( nsectors ) { blk = nblk++; dprintf("querying block %u\n", blk); if ( ioctl(fd, FIBMAP, &blk) ) return -1; blk *= blksize; for ( i = 0 ; i < blksize ; i++ ) { if ( !nsectors ) return 0; dprintf("Sector: %10u\n", blk); *sectors++ = blk++; nsectors--; } } return 0;}/* * Get the size of a block device */uint64_t get_size(int devfd){ uint64_t bytes; uint32_t sects; struct stat st;#ifdef BLKGETSIZE64 if ( !ioctl(devfd, BLKGETSIZE64, &bytes) ) return bytes;#endif if ( !ioctl(devfd, BLKGETSIZE, §s) ) return (uint64_t)sects << 9; else if ( !fstat(devfd, &st) && st.st_size ) return st.st_size; else return 0;}/* * Get device geometry and partition offset */struct geometry_table { uint64_t bytes; struct hd_geometry g;};/* Standard floppy disk geometries, plus LS-120. Zipdisk geometry (x/64/32) is the final fallback. I don't know what LS-240 has as its geometry, since I don't have one and don't know anyone that does, and Google wasn't helpful... */static const struct geometry_table standard_geometries[] = { { 360*1024, { 2, 9, 40, 0 } }, { 720*1024, { 2, 9, 80, 0 } }, { 1200*1024, { 2, 15, 80, 0 } }, { 1440*1024, { 2, 18, 80, 0 } }, { 1680*1024, { 2, 21, 80, 0 } }, { 1722*1024, { 2, 21, 80, 0 } }, { 2880*1024, { 2, 36, 80, 0 } }, { 3840*1024, { 2, 48, 80, 0 } }, { 123264*1024, { 8, 32, 963, 0 } }, /* LS120 */ { 0, {0,0,0,0} }};intget_geometry(int devfd, uint64_t totalbytes, struct hd_geometry *geo){ struct floppy_struct fd_str; const struct geometry_table *gp; memset(geo, 0, sizeof *geo);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -