(2006-08-06) rescue-bootcd

This commit is contained in:
2006-08-06 00:00:00 +02:00
parent 2f796b816a
commit decb062d20
21091 changed files with 7076462 additions and 0 deletions

View File

@@ -0,0 +1 @@
extlinux.o: extlinux.c ext2_fs.h ../version.h

View File

@@ -0,0 +1 @@
extlinux_bss_bin.o: ../extlinux_bss_bin.c

View File

@@ -0,0 +1 @@
extlinux_sys_bin.o: ../extlinux_sys_bin.c

View File

@@ -0,0 +1,40 @@
CC = gcc
OPTFLAGS = -g -Os
INCLUDES = -I. -I.. -I../libfat
CFLAGS = -W -Wall -Wno-sign-compare -D_FILE_OFFSET_BITS=64 $(OPTFLAGS) $(INCLUDES)
LDFLAGS = -s
SRCS = extlinux.c ../extlinux_bss_bin.c ../extlinux_sys_bin.c
OBJS = $(patsubst %.c,%.o,$(notdir $(SRCS)))
.SUFFIXES: .c .o .i .s .S
VPATH = .:..
all: installer
tidy:
-rm -f *.o *.i *.s *.a .*.d
clean: tidy
-rm -f extlinux
spotless: clean
-rm -f *~
installer: extlinux
extlinux: $(OBJS)
$(CC) $(LDFLAGS) -o $@ $^
%.o: %.c
$(CC) -Wp,-MT,$@,-MMD,.$@.d $(CFLAGS) -c -o $@ $<
%.i: %.c
$(CC) $(CFLAGS) -E -o $@ $<
%.s: %.c
$(CC) $(CFLAGS) -S -o $@ $<
-include .*.d

View File

@@ -0,0 +1,508 @@
/*
* linux/include/linux/ext2_fs.h
*
* Copyright (C) 1992, 1993, 1994, 1995
* Remy Card (card@masi.ibp.fr)
* Laboratoire MASI - Institut Blaise Pascal
* Universite Pierre et Marie Curie (Paris VI)
*
* from
*
* linux/include/linux/minix_fs.h
*
* Copyright (C) 1991, 1992 Linus Torvalds
*/
/* Private copy for extlinux */
#ifndef _LINUX_EXT2_FS_H
#define _LINUX_EXT2_FS_H
#include <linux/types.h>
/*
* The second extended filesystem constants/structures
*/
/*
* Define EXT2FS_DEBUG to produce debug messages
*/
#undef EXT2FS_DEBUG
/*
* Define EXT2_PREALLOCATE to preallocate data blocks for expanding files
*/
#define EXT2_PREALLOCATE
#define EXT2_DEFAULT_PREALLOC_BLOCKS 8
/*
* The second extended file system version
*/
#define EXT2FS_DATE "95/08/09"
#define EXT2FS_VERSION "0.5b"
/*
* Debug code
*/
#ifdef EXT2FS_DEBUG
# define ext2_debug(f, a...) { \
printk ("EXT2-fs DEBUG (%s, %d): %s:", \
__FILE__, __LINE__, __FUNCTION__); \
printk (f, ## a); \
}
#else
# define ext2_debug(f, a...) /**/
#endif
/*
* Special inode numbers
*/
#define EXT2_BAD_INO 1 /* Bad blocks inode */
#define EXT2_ROOT_INO 2 /* Root inode */
#define EXT2_ACL_IDX_INO 3 /* ACL inode */
#define EXT2_ACL_DATA_INO 4 /* ACL inode */
#define EXT2_BOOT_LOADER_INO 5 /* Boot loader inode */
#define EXT2_UNDEL_DIR_INO 6 /* Undelete directory inode */
/* First non-reserved inode for old ext2 filesystems */
#define EXT2_GOOD_OLD_FIRST_INO 11
/*
* The second extended file system magic number
*/
#define EXT2_SUPER_MAGIC 0xEF53
/*
* Maximal count of links to a file
*/
#define EXT2_LINK_MAX 32000
/*
* Macro-instructions used to manage several block sizes
*/
#define EXT2_MIN_BLOCK_SIZE 1024
#define EXT2_MAX_BLOCK_SIZE 4096
#define EXT2_MIN_BLOCK_LOG_SIZE 10
# define EXT2_BLOCK_SIZE(s) (EXT2_MIN_BLOCK_SIZE << (s)->s_log_block_size)
#define EXT2_ACLE_PER_BLOCK(s) (EXT2_BLOCK_SIZE(s) / sizeof (struct ext2_acl_entry))
#define EXT2_ADDR_PER_BLOCK(s) (EXT2_BLOCK_SIZE(s) / sizeof (__u32))
# define EXT2_BLOCK_SIZE_BITS(s) ((s)->s_log_block_size + 10)
#define EXT2_INODE_SIZE(s) (((s)->s_rev_level == EXT2_GOOD_OLD_REV) ? \
EXT2_GOOD_OLD_INODE_SIZE : \
(s)->s_inode_size)
#define EXT2_FIRST_INO(s) (((s)->s_rev_level == EXT2_GOOD_OLD_REV) ? \
EXT2_GOOD_OLD_FIRST_INO : \
(s)->s_first_ino)
/*
* Macro-instructions used to manage fragments
*/
#define EXT2_MIN_FRAG_SIZE 1024
#define EXT2_MAX_FRAG_SIZE 4096
#define EXT2_MIN_FRAG_LOG_SIZE 10
# define EXT2_FRAG_SIZE(s) (EXT2_MIN_FRAG_SIZE << (s)->s_log_frag_size)
# define EXT2_FRAGS_PER_BLOCK(s) (EXT2_BLOCK_SIZE(s) / EXT2_FRAG_SIZE(s))
/*
* ACL structures
*/
struct ext2_acl_header /* Header of Access Control Lists */
{
__u32 aclh_size;
__u32 aclh_file_count;
__u32 aclh_acle_count;
__u32 aclh_first_acle;
};
struct ext2_acl_entry /* Access Control List Entry */
{
__u32 acle_size;
__u16 acle_perms; /* Access permissions */
__u16 acle_type; /* Type of entry */
__u16 acle_tag; /* User or group identity */
__u16 acle_pad1;
__u32 acle_next; /* Pointer on next entry for the */
/* same inode or on next free entry */
};
/*
* Structure of a blocks group descriptor
*/
struct ext2_group_desc
{
__u32 bg_block_bitmap; /* Blocks bitmap block */
__u32 bg_inode_bitmap; /* Inodes bitmap block */
__u32 bg_inode_table; /* Inodes table block */
__u16 bg_free_blocks_count; /* Free blocks count */
__u16 bg_free_inodes_count; /* Free inodes count */
__u16 bg_used_dirs_count; /* Directories count */
__u16 bg_pad;
__u32 bg_reserved[3];
};
/*
* Macro-instructions used to manage group descriptors
*/
# define EXT2_BLOCKS_PER_GROUP(s) ((s)->s_blocks_per_group)
# define EXT2_DESC_PER_BLOCK(s) (EXT2_BLOCK_SIZE(s) / sizeof (struct ext2_group_desc))
# define EXT2_INODES_PER_GROUP(s) ((s)->s_inodes_per_group)
/*
* Constants relative to the data blocks
*/
#define EXT2_NDIR_BLOCKS 12
#define EXT2_IND_BLOCK EXT2_NDIR_BLOCKS
#define EXT2_DIND_BLOCK (EXT2_IND_BLOCK + 1)
#define EXT2_TIND_BLOCK (EXT2_DIND_BLOCK + 1)
#define EXT2_N_BLOCKS (EXT2_TIND_BLOCK + 1)
/*
* Inode flags
*/
#define EXT2_SECRM_FL 0x00000001 /* Secure deletion */
#define EXT2_UNRM_FL 0x00000002 /* Undelete */
#define EXT2_COMPR_FL 0x00000004 /* Compress file */
#define EXT2_SYNC_FL 0x00000008 /* Synchronous updates */
#define EXT2_IMMUTABLE_FL 0x00000010 /* Immutable file */
#define EXT2_APPEND_FL 0x00000020 /* writes to file may only append */
#define EXT2_NODUMP_FL 0x00000040 /* do not dump file */
#define EXT2_NOATIME_FL 0x00000080 /* do not update atime */
/* Reserved for compression usage... */
#define EXT2_DIRTY_FL 0x00000100
#define EXT2_COMPRBLK_FL 0x00000200 /* One or more compressed clusters */
#define EXT2_NOCOMP_FL 0x00000400 /* Don't compress */
#define EXT2_ECOMPR_FL 0x00000800 /* Compression error */
/* End compression flags --- maybe not all used */
#define EXT2_BTREE_FL 0x00001000 /* btree format dir */
#define EXT2_RESERVED_FL 0x80000000 /* reserved for ext2 lib */
#define EXT2_FL_USER_VISIBLE 0x00001FFF /* User visible flags */
#define EXT2_FL_USER_MODIFIABLE 0x000000FF /* User modifiable flags */
/*
* ioctl commands
*/
#define EXT2_IOC_GETFLAGS _IOR('f', 1, long)
#define EXT2_IOC_SETFLAGS _IOW('f', 2, long)
#define EXT2_IOC_GETVERSION _IOR('v', 1, long)
#define EXT2_IOC_SETVERSION _IOW('v', 2, long)
/*
* Structure of an inode on the disk
*/
struct ext2_inode {
__u16 i_mode; /* File mode */
__u16 i_uid; /* Low 16 bits of Owner Uid */
__u32 i_size; /* Size in bytes */
__u32 i_atime; /* Access time */
__u32 i_ctime; /* Creation time */
__u32 i_mtime; /* Modification time */
__u32 i_dtime; /* Deletion Time */
__u16 i_gid; /* Low 16 bits of Group Id */
__u16 i_links_count; /* Links count */
__u32 i_blocks; /* Blocks count */
__u32 i_flags; /* File flags */
union {
struct {
__u32 l_i_reserved1;
} linux1;
struct {
__u32 h_i_translator;
} hurd1;
struct {
__u32 m_i_reserved1;
} masix1;
} osd1; /* OS dependent 1 */
__u32 i_block[EXT2_N_BLOCKS];/* Pointers to blocks */
__u32 i_generation; /* File version (for NFS) */
__u32 i_file_acl; /* File ACL */
__u32 i_dir_acl; /* Directory ACL */
__u32 i_faddr; /* Fragment address */
union {
struct {
__u8 l_i_frag; /* Fragment number */
__u8 l_i_fsize; /* Fragment size */
__u16 i_pad1;
__u16 l_i_uid_high; /* these 2 fields */
__u16 l_i_gid_high; /* were reserved2[0] */
__u32 l_i_reserved2;
} linux2;
struct {
__u8 h_i_frag; /* Fragment number */
__u8 h_i_fsize; /* Fragment size */
__u16 h_i_mode_high;
__u16 h_i_uid_high;
__u16 h_i_gid_high;
__u32 h_i_author;
} hurd2;
struct {
__u8 m_i_frag; /* Fragment number */
__u8 m_i_fsize; /* Fragment size */
__u16 m_pad1;
__u32 m_i_reserved2[2];
} masix2;
} osd2; /* OS dependent 2 */
};
#define i_size_high i_dir_acl
#if defined(__KERNEL__) || defined(__linux__)
#define i_reserved1 osd1.linux1.l_i_reserved1
#define i_frag osd2.linux2.l_i_frag
#define i_fsize osd2.linux2.l_i_fsize
#define i_uid_low i_uid
#define i_gid_low i_gid
#define i_uid_high osd2.linux2.l_i_uid_high
#define i_gid_high osd2.linux2.l_i_gid_high
#define i_reserved2 osd2.linux2.l_i_reserved2
#endif
#ifdef __hurd__
#define i_translator osd1.hurd1.h_i_translator
#define i_frag osd2.hurd2.h_i_frag;
#define i_fsize osd2.hurd2.h_i_fsize;
#define i_uid_high osd2.hurd2.h_i_uid_high
#define i_gid_high osd2.hurd2.h_i_gid_high
#define i_author osd2.hurd2.h_i_author
#endif
#ifdef __masix__
#define i_reserved1 osd1.masix1.m_i_reserved1
#define i_frag osd2.masix2.m_i_frag
#define i_fsize osd2.masix2.m_i_fsize
#define i_reserved2 osd2.masix2.m_i_reserved2
#endif
/*
* File system states
*/
#define EXT2_VALID_FS 0x0001 /* Unmounted cleanly */
#define EXT2_ERROR_FS 0x0002 /* Errors detected */
/*
* Mount flags
*/
#define EXT2_MOUNT_CHECK 0x0001 /* Do mount-time checks */
#define EXT2_MOUNT_GRPID 0x0004 /* Create files with directory's group */
#define EXT2_MOUNT_DEBUG 0x0008 /* Some debugging messages */
#define EXT2_MOUNT_ERRORS_CONT 0x0010 /* Continue on errors */
#define EXT2_MOUNT_ERRORS_RO 0x0020 /* Remount fs ro on errors */
#define EXT2_MOUNT_ERRORS_PANIC 0x0040 /* Panic on errors */
#define EXT2_MOUNT_MINIX_DF 0x0080 /* Mimics the Minix statfs */
#define EXT2_MOUNT_NO_UID32 0x0200 /* Disable 32-bit UIDs */
#define clear_opt(o, opt) o &= ~EXT2_MOUNT_##opt
#define set_opt(o, opt) o |= EXT2_MOUNT_##opt
#define test_opt(sb, opt) ((sb)->u.ext2_sb.s_mount_opt & \
EXT2_MOUNT_##opt)
/*
* Maximal mount counts between two filesystem checks
*/
#define EXT2_DFL_MAX_MNT_COUNT 20 /* Allow 20 mounts */
#define EXT2_DFL_CHECKINTERVAL 0 /* Don't use interval check */
/*
* Behaviour when detecting errors
*/
#define EXT2_ERRORS_CONTINUE 1 /* Continue execution */
#define EXT2_ERRORS_RO 2 /* Remount fs read-only */
#define EXT2_ERRORS_PANIC 3 /* Panic */
#define EXT2_ERRORS_DEFAULT EXT2_ERRORS_CONTINUE
/*
* Structure of the super block
*/
struct ext2_super_block {
__u32 s_inodes_count; /* Inodes count */
__u32 s_blocks_count; /* Blocks count */
__u32 s_r_blocks_count; /* Reserved blocks count */
__u32 s_free_blocks_count; /* Free blocks count */
__u32 s_free_inodes_count; /* Free inodes count */
__u32 s_first_data_block; /* First Data Block */
__u32 s_log_block_size; /* Block size */
__s32 s_log_frag_size; /* Fragment size */
__u32 s_blocks_per_group; /* # Blocks per group */
__u32 s_frags_per_group; /* # Fragments per group */
__u32 s_inodes_per_group; /* # Inodes per group */
__u32 s_mtime; /* Mount time */
__u32 s_wtime; /* Write time */
__u16 s_mnt_count; /* Mount count */
__s16 s_max_mnt_count; /* Maximal mount count */
__u16 s_magic; /* Magic signature */
__u16 s_state; /* File system state */
__u16 s_errors; /* Behaviour when detecting errors */
__u16 s_minor_rev_level; /* minor revision level */
__u32 s_lastcheck; /* time of last check */
__u32 s_checkinterval; /* max. time between checks */
__u32 s_creator_os; /* OS */
__u32 s_rev_level; /* Revision level */
__u16 s_def_resuid; /* Default uid for reserved blocks */
__u16 s_def_resgid; /* Default gid for reserved blocks */
/*
* These fields are for EXT2_DYNAMIC_REV superblocks only.
*
* Note: the difference between the compatible feature set and
* the incompatible feature set is that if there is a bit set
* in the incompatible feature set that the kernel doesn't
* know about, it should refuse to mount the filesystem.
*
* e2fsck's requirements are more strict; if it doesn't know
* about a feature in either the compatible or incompatible
* feature set, it must abort and not try to meddle with
* things it doesn't understand...
*/
__u32 s_first_ino; /* First non-reserved inode */
__u16 s_inode_size; /* size of inode structure */
__u16 s_block_group_nr; /* block group # of this superblock */
__u32 s_feature_compat; /* compatible feature set */
__u32 s_feature_incompat; /* incompatible feature set */
__u32 s_feature_ro_compat; /* readonly-compatible feature set */
__u8 s_uuid[16]; /* 128-bit uuid for volume */
char s_volume_name[16]; /* volume name */
char s_last_mounted[64]; /* directory where last mounted */
__u32 s_algorithm_usage_bitmap; /* For compression */
/*
* Performance hints. Directory preallocation should only
* happen if the EXT2_COMPAT_PREALLOC flag is on.
*/
__u8 s_prealloc_blocks; /* Nr of blocks to try to preallocate*/
__u8 s_prealloc_dir_blocks; /* Nr to preallocate for dirs */
__u16 s_padding1;
__u32 s_reserved[204]; /* Padding to the end of the block */
};
/* Assume that user mode programs are passing in an ext2fs superblock, not
* a kernel struct super_block. This will allow us to call the feature-test
* macros from user land. */
#define EXT2_SB(sb) (sb)
/*
* Codes for operating systems
*/
#define EXT2_OS_LINUX 0
#define EXT2_OS_HURD 1
#define EXT2_OS_MASIX 2
#define EXT2_OS_FREEBSD 3
#define EXT2_OS_LITES 4
/*
* Revision levels
*/
#define EXT2_GOOD_OLD_REV 0 /* The good old (original) format */
#define EXT2_DYNAMIC_REV 1 /* V2 format w/ dynamic inode sizes */
#define EXT2_CURRENT_REV EXT2_GOOD_OLD_REV
#define EXT2_MAX_SUPP_REV EXT2_DYNAMIC_REV
#define EXT2_GOOD_OLD_INODE_SIZE 128
/*
* Feature set definitions
*/
#define EXT2_HAS_COMPAT_FEATURE(sb,mask) \
( EXT2_SB(sb)->s_es->s_feature_compat & cpu_to_le32(mask) )
#define EXT2_HAS_RO_COMPAT_FEATURE(sb,mask) \
( EXT2_SB(sb)->s_es->s_feature_ro_compat & cpu_to_le32(mask) )
#define EXT2_HAS_INCOMPAT_FEATURE(sb,mask) \
( EXT2_SB(sb)->s_es->s_feature_incompat & cpu_to_le32(mask) )
#define EXT2_SET_COMPAT_FEATURE(sb,mask) \
EXT2_SB(sb)->s_es->s_feature_compat |= cpu_to_le32(mask)
#define EXT2_SET_RO_COMPAT_FEATURE(sb,mask) \
EXT2_SB(sb)->s_es->s_feature_ro_compat |= cpu_to_le32(mask)
#define EXT2_SET_INCOMPAT_FEATURE(sb,mask) \
EXT2_SB(sb)->s_es->s_feature_incompat |= cpu_to_le32(mask)
#define EXT2_CLEAR_COMPAT_FEATURE(sb,mask) \
EXT2_SB(sb)->s_es->s_feature_compat &= ~cpu_to_le32(mask)
#define EXT2_CLEAR_RO_COMPAT_FEATURE(sb,mask) \
EXT2_SB(sb)->s_es->s_feature_ro_compat &= ~cpu_to_le32(mask)
#define EXT2_CLEAR_INCOMPAT_FEATURE(sb,mask) \
EXT2_SB(sb)->s_es->s_feature_incompat &= ~cpu_to_le32(mask)
#define EXT2_FEATURE_COMPAT_DIR_PREALLOC 0x0001
#define EXT2_FEATURE_COMPAT_IMAGIC_INODES 0x0002
#define EXT3_FEATURE_COMPAT_HAS_JOURNAL 0x0004
#define EXT2_FEATURE_COMPAT_EXT_ATTR 0x0008
#define EXT2_FEATURE_COMPAT_RESIZE_INO 0x0010
#define EXT2_FEATURE_COMPAT_DIR_INDEX 0x0020
#define EXT2_FEATURE_COMPAT_ANY 0xffffffff
#define EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER 0x0001
#define EXT2_FEATURE_RO_COMPAT_LARGE_FILE 0x0002
#define EXT2_FEATURE_RO_COMPAT_BTREE_DIR 0x0004
#define EXT2_FEATURE_RO_COMPAT_ANY 0xffffffff
#define EXT2_FEATURE_INCOMPAT_COMPRESSION 0x0001
#define EXT2_FEATURE_INCOMPAT_FILETYPE 0x0002
#define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004
#define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008
#define EXT2_FEATURE_INCOMPAT_ANY 0xffffffff
#define EXT2_FEATURE_COMPAT_SUPP 0
#define EXT2_FEATURE_INCOMPAT_SUPP EXT2_FEATURE_INCOMPAT_FILETYPE
#define EXT2_FEATURE_RO_COMPAT_SUPP (EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER| \
EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \
EXT2_FEATURE_RO_COMPAT_BTREE_DIR)
#define EXT2_FEATURE_RO_COMPAT_UNSUPPORTED ~EXT2_FEATURE_RO_COMPAT_SUPP
#define EXT2_FEATURE_INCOMPAT_UNSUPPORTED ~EXT2_FEATURE_INCOMPAT_SUPP
/*
* Default values for user and/or group using reserved blocks
*/
#define EXT2_DEF_RESUID 0
#define EXT2_DEF_RESGID 0
/*
* Structure of a directory entry
*/
#define EXT2_NAME_LEN 255
struct ext2_dir_entry {
__u32 inode; /* Inode number */
__u16 rec_len; /* Directory entry length */
__u16 name_len; /* Name length */
char name[EXT2_NAME_LEN]; /* File name */
};
/*
* The new version of the directory entry. Since EXT2 structures are
* stored in intel byte order, and the name_len field could never be
* bigger than 255 chars, it's safe to reclaim the extra byte for the
* file_type field.
*/
struct ext2_dir_entry_2 {
__u32 inode; /* Inode number */
__u16 rec_len; /* Directory entry length */
__u8 name_len; /* Name length */
__u8 file_type;
char name[EXT2_NAME_LEN]; /* File name */
};
/*
* Ext2 directory file types. Only the low 3 bits are used. The
* other bits are reserved for now.
*/
enum {
EXT2_FT_UNKNOWN,
EXT2_FT_REG_FILE,
EXT2_FT_DIR,
EXT2_FT_CHRDEV,
EXT2_FT_BLKDEV,
EXT2_FT_FIFO,
EXT2_FT_SOCK,
EXT2_FT_SYMLINK,
EXT2_FT_MAX
};
/*
* EXT2_DIR_PAD defines the directory entries boundaries
*
* NOTE: It must be a multiple of 4
*/
#define EXT2_DIR_PAD 4
#define EXT2_DIR_ROUND (EXT2_DIR_PAD - 1)
#define EXT2_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT2_DIR_ROUND) & \
~EXT2_DIR_ROUND)
#endif /* _LINUX_EXT2_FS_H */

Binary file not shown.

View File

@@ -0,0 +1,756 @@
#ident "$Id: extlinux.c,v 1.15 2005/04/03 00:00:36 hpa Exp $"
/* ----------------------------------------------------------------------- *
*
* 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"
" --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[] = {
{ "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[] = "zS: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 0x3eb202fe
enum 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
*/
int
sectmap(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, &sects) )
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} }
};
int
get_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);
if ( !ioctl(devfd, HDIO_GETGEO, &geo) ) {
return 0;
} else if ( !ioctl(devfd, FDGETPRM, &fd_str) ) {
geo->heads = fd_str.head;
geo->sectors = fd_str.sect;
geo->cylinders = fd_str.track;
geo->start = 0;
return 0;
}
/* Didn't work. Let's see if this is one of the standard geometries */
for ( gp = standard_geometries ; gp->bytes ; gp++ ) {
if ( gp->bytes == totalbytes ) {
memcpy(geo, &gp->g, sizeof *geo);
return 0;
}
}
/* Didn't work either... assign a geometry of 64 heads, 32 sectors; this is
what zipdisks use, so this would help if someone has a USB key that
they're booting in USB-ZIP mode. */
geo->heads = opt.heads ?: 64;
geo->sectors = opt.sectors ?: 32;
geo->cylinders = totalbytes/(geo->heads*geo->sectors << SECTOR_SHIFT);
geo->start = 0;
if ( !opt.sectors && !opt.heads )
fprintf(stderr, "Warning: unable to obtain device geometry (defaulting to %d heads, %d sectors)\n"
" (on hard disks, this is usually harmless.)\n",
geo->heads, geo->sectors);
return 1;
}
/*
* Query the device geometry and put it into the boot sector.
* Map the file and put the map in the boot sector and file.
* Stick the "current directory" inode number into the file.
*/
void
patch_file_and_bootblock(int fd, int dirfd, int devfd)
{
struct stat dirst;
struct hd_geometry geo;
uint32_t *sectp;
uint64_t totalbytes, totalsectors;
int nsect;
unsigned char *p, *patcharea;
int i, dw;
uint32_t csum;
if ( fstat(dirfd, &dirst) ) {
perror("fstat dirfd");
exit(255); /* This should never happen */
}
totalbytes = get_size(devfd);
get_geometry(devfd, totalbytes, &geo);
if ( opt.heads )
geo.heads = opt.heads;
if ( opt.sectors )
geo.sectors = opt.sectors;
/* Patch this into a fake FAT superblock. This isn't because
FAT is a good format in any way, it's because it lets the
early bootstrap share code with the FAT version. */
dprintf("heads = %u, sect = %u\n", geo.heads, geo.sectors);
totalsectors = totalbytes >> SECTOR_SHIFT;
if ( totalsectors >= 65536 ) {
set_16(boot_block+bsSectors, 0);
} else {
set_16(boot_block+bsSectors, totalsectors);
}
set_32(boot_block+bsHugeSectors, totalsectors);
set_16(boot_block+bsBytesPerSec, SECTOR_SIZE);
set_16(boot_block+bsSecPerTrack, geo.sectors);
set_16(boot_block+bsHeads, geo.heads);
set_32(boot_block+bsHiddenSecs, geo.start);
/* Construct the boot file */
dprintf("directory inode = %lu\n", (unsigned long) dirst.st_ino);
nsect = (boot_image_len+SECTOR_SIZE-1) >> SECTOR_SHIFT;
sectp = alloca(sizeof(uint32_t)*nsect);
if ( sectmap(fd, sectp, nsect) ) {
perror("bmap");
exit(1);
}
/* First sector need pointer in boot sector */
set_32(boot_block+0x1F8, *sectp++);
nsect--;
/* Search for LDLINUX_MAGIC to find the patch area */
for ( p = boot_image ; get_32(p) != LDLINUX_MAGIC ; p += 4 );
patcharea = p+8;
/* Set up the totals */
dw = boot_image_len >> 2; /* COMPLETE dwords! */
set_16(patcharea, dw);
set_16(patcharea+2, nsect); /* Does not include the first sector! */
set_32(patcharea+8, dirst.st_ino); /* "Current" directory */
/* Set the sector pointers */
p = patcharea+12;
memset(p, 0, 64*4);
while ( nsect-- ) {
set_32(p, *sectp++);
p += 4;
}
/* Now produce a checksum */
set_32(patcharea+4, 0);
csum = LDLINUX_MAGIC;
for ( i = 0, p = boot_image ; i < dw ; i++, p += 4 )
csum -= get_32(p); /* Negative checksum */
set_32(patcharea+4, csum);
}
/*
* Install the boot block on the specified device.
* Must be run AFTER install_file()!
*/
int
install_bootblock(int fd, const char *device)
{
struct ext2_super_block sb;
if ( xpread(fd, &sb, sizeof sb, EXT2_SUPER_OFFSET) != sizeof sb ) {
perror("reading superblock");
return 1;
}
if ( sb.s_magic != EXT2_SUPER_MAGIC ) {
fprintf(stderr, "no ext2/ext3 superblock found on %s\n", device);
return 1;
}
if ( xpwrite(fd, boot_block, boot_block_len, 0) != boot_block_len ) {
perror("writing bootblock");
return 1;
}
return 0;
}
int
install_file(const char *path, int devfd, struct stat *rst)
{
char *file;
int fd = -1, dirfd = -1, flags;
struct stat st;
asprintf(&file, "%s%sextlinux.sys",
path,
path[0] && path[strlen(path)-1] == '/' ? "" : "/");
if ( !file ) {
perror(program);
return 1;
}
dirfd = open(path, O_RDONLY|O_DIRECTORY);
if ( dirfd < 0 ) {
perror(path);
goto bail;
}
fd = open(file, O_RDONLY);
if ( fd < 0 ) {
if ( errno != ENOENT ) {
perror(file);
goto bail;
}
} else {
/* If file exist, remove the immutable flag and set u+w mode */
if ( !ioctl(fd, EXT2_IOC_GETFLAGS, &flags) ) {
flags &= ~EXT2_IMMUTABLE_FL;
ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
}
if ( !fstat(fd, &st) ) {
fchmod(fd, st.st_mode | S_IWUSR);
}
}
close(fd);
fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, S_IRUSR|S_IRGRP|S_IROTH);
if ( fd < 0 ) {
perror(file);
goto bail;
}
/* Write it the first time */
if ( xpwrite(fd, boot_image, boot_image_len, 0) != boot_image_len ) {
fprintf(stderr, "%s: write failure on %s\n", program, file);
goto bail;
}
/* Map the file, and patch the initial sector accordingly */
patch_file_and_bootblock(fd, dirfd, devfd);
/* Write it again - this relies on the file being overwritten in place! */
if ( xpwrite(fd, boot_image, boot_image_len, 0) != boot_image_len ) {
fprintf(stderr, "%s: write failure on %s\n", program, file);
goto bail;
}
/* Attempt to set immutable flag and remove all write access */
/* Only set immutable flag if file is owned by root */
if ( !fstat(fd, &st) ) {
fchmod(fd, st.st_mode & (S_IRUSR|S_IRGRP|S_IROTH));
if ( st.st_uid == 0 && !ioctl(fd, EXT2_IOC_GETFLAGS, &flags) ) {
flags |= EXT2_IMMUTABLE_FL;
ioctl(fd, EXT2_IOC_SETFLAGS, &flags);
}
}
if ( fstat(fd, rst) ) {
perror(file);
goto bail;
}
close(dirfd);
close(fd);
return 0;
bail:
if ( dirfd >= 0 )
close(dirfd);
if ( fd >= 0 )
close(fd);
return 1;
}
int
install_loader(const char *path)
{
struct stat st, dst, fst;
struct mntent *mnt = NULL;
int devfd, rv;
FILE *mtab;
if ( stat(path, &st) || !S_ISDIR(st.st_mode) ) {
fprintf(stderr, "%s: Not a directory: %s\n", program, path);
return 1;
}
devfd = -1;
if ( (mtab = setmntent("/proc/mounts", "r")) ) {
while ( (mnt = getmntent(mtab)) ) {
if ( (!strcmp(mnt->mnt_type, "ext2") ||
!strcmp(mnt->mnt_type, "ext3")) &&
!stat(mnt->mnt_fsname, &dst) &&
dst.st_rdev == st.st_dev ) {
fprintf(stderr, "%s is device %s\n", path, mnt->mnt_fsname);
if ( (devfd = open(mnt->mnt_fsname, O_RDWR|O_SYNC)) < 0 ) {
fprintf(stderr, "%s: cannot open device %s\n", program, mnt->mnt_fsname);
return 1;
}
break;
}
}
}
if ( devfd < 0 ) {
/* Didn't find it in /proc/mounts, try /etc/mtab */
if ( (mtab = setmntent("/etc/mtab", "r")) ) {
while ( (mnt = getmntent(mtab)) ) {
if ( (!strcmp(mnt->mnt_type, "ext2") ||
!strcmp(mnt->mnt_type, "ext3")) &&
!stat(mnt->mnt_fsname, &dst) &&
dst.st_rdev == st.st_dev ) {
fprintf(stderr, "%s is device %s\n", path, mnt->mnt_fsname);
if ( (devfd = open(mnt->mnt_fsname, O_RDWR|O_SYNC)) < 0 ) {
fprintf(stderr, "%s: cannot open device %s\n", program, mnt->mnt_fsname);
return 1;
}
break;
}
}
}
}
if ( devfd < 0 ) {
fprintf(stderr, "%s: cannot find device for path %s\n", program, path);
return 1;
}
install_file(path, devfd, &fst);
if ( fst.st_dev != st.st_dev ) {
fprintf(stderr, "%s: file system changed under us - aborting!\n",
program);
return 1;
}
sync();
rv = install_bootblock(devfd, mnt->mnt_fsname);
close(devfd);
sync();
endmntent(mtab);
if ( rv ) return rv;
return 0;
}
int
main(int argc, char *argv[])
{
int o;
const char *directory;
program = argv[0];
while ( (o = getopt_long(argc, argv, short_options,
long_options, NULL)) != EOF ) {
switch ( o ) {
case 'z':
opt.heads = 64;
opt.sectors = 32;
break;
case 'S':
opt.sectors = strtoul(optarg, NULL, 0);
if ( opt.sectors < 1 || opt.sectors > 63 ) {
fprintf(stderr, "%s: invalid number of sectors: %u (must be 1-63)\n",
program, opt.sectors);
exit(EX_USAGE);
}
break;
case 'H':
opt.heads = strtoul(optarg, NULL, 0);
if ( opt.heads < 1 || opt.heads > 256 ) {
fprintf(stderr, "%s: invalid number of heads: %u (must be 1-256)\n",
program, opt.heads);
exit(EX_USAGE);
}
break;
case 'h':
usage(0);
break;
case 'v':
fputs("extlinux " VERSION "\n", stderr);
exit(0);
default:
fprintf(stderr, "%s: Unknown option: %c\n", program, optopt);
exit(EX_USAGE);
}
}
directory = argv[optind];
if ( !directory )
usage(EX_USAGE);
return install_loader(directory);
}

Binary file not shown.

Binary file not shown.

Binary file not shown.