summaryrefslogtreecommitdiff
path: root/package/aboot/src/tools
diff options
context:
space:
mode:
authorWaldemar Brodkorb <wbx@openadk.org>2017-12-31 18:47:16 +0100
committerWaldemar Brodkorb <wbx@openadk.org>2017-12-31 18:47:25 +0100
commit3a96085b999220c4da0c5ef7d1f7ba26b9ddfb98 (patch)
tree77f1445aae2e6be5135594e95986b3278bbc061c /package/aboot/src/tools
parentcc28479164b8dc8afd4310716da32f16022f5974 (diff)
dec-multia: make netboot possible, add aboot bootloader
Diffstat (limited to 'package/aboot/src/tools')
-rw-r--r--package/aboot/src/tools/.cvsignore5
-rw-r--r--package/aboot/src/tools/Makefile22
-rw-r--r--package/aboot/src/tools/abootconf.c125
-rw-r--r--package/aboot/src/tools/bio.c178
-rw-r--r--package/aboot/src/tools/bio.h4
-rw-r--r--package/aboot/src/tools/e2lib.c1473
-rw-r--r--package/aboot/src/tools/e2lib.h332
-rw-r--r--package/aboot/src/tools/e2writeboot.c254
-rw-r--r--package/aboot/src/tools/elfencap.c61
-rw-r--r--package/aboot/src/tools/isomarkboot.c239
-rw-r--r--package/aboot/src/tools/objstrip.c260
11 files changed, 2953 insertions, 0 deletions
diff --git a/package/aboot/src/tools/.cvsignore b/package/aboot/src/tools/.cvsignore
new file mode 100644
index 000000000..2197455e0
--- /dev/null
+++ b/package/aboot/src/tools/.cvsignore
@@ -0,0 +1,5 @@
+abootconf
+e2writeboot
+elfencap
+isomarkboot
+objstrip
diff --git a/package/aboot/src/tools/Makefile b/package/aboot/src/tools/Makefile
new file mode 100644
index 000000000..740b9cb9d
--- /dev/null
+++ b/package/aboot/src/tools/Makefile
@@ -0,0 +1,22 @@
+ifndef ($(CC))
+CC = gcc
+endif
+override CFLAGS = -g -O2 -Wall -I. -I../include $(CPPFLAGS)
+override LDFLAGS = -g
+override PGMS += e2writeboot abootconf elfencap objstrip
+
+EXEC_PREFIX = /usr
+
+all: $(PGMS)
+
+install: $(PGMS)
+ install -c -o root -g root -m 755 $(PGMS) $(EXEC_PREFIX)/bin
+
+clean:
+ rm -f *~ *.o *.a core $(PGMS)
+
+isomarkboot: isomarkboot.o ../lib/isolib.o
+e2writeboot: e2writeboot.o e2lib.o bio.o
+
+e2writeboot.o: e2lib.h
+e2lib.o: e2lib.h
diff --git a/package/aboot/src/tools/abootconf.c b/package/aboot/src/tools/abootconf.c
new file mode 100644
index 000000000..0d0ae075c
--- /dev/null
+++ b/package/aboot/src/tools/abootconf.c
@@ -0,0 +1,125 @@
+/*
+ * abootconf.c
+ *
+ * This file is part of aboot, the SRM bootloader for Linux/Alpha
+ * Copyright (C) 1996 Linus Torvalds, David Mosberger, and Michael Schwingen.
+ *
+ * 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; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <sys/fcntl.h>
+
+#include <config.h>
+
+
+const char * prog_name;
+
+int
+main (int argc, char ** argv)
+{
+ unsigned long sector[512 / sizeof(unsigned long)];
+ off_t aboot_pos;
+ size_t nbytes;
+ long part = -1;
+ int disk, i;
+
+ prog_name = argv[0];
+
+ if (argc != 2 && argc != 3) {
+ fprintf(stderr, "usage: %s device [partition]\n", prog_name);
+ exit(1);
+ }
+
+ if (argc > 2) {
+ errno = 0;
+ part = strtol(argv[2], 0, 10);
+ if (errno == ERANGE) {
+ fprintf(stderr, "%s: bad partition number %s\n",
+ prog_name, argv[2]);
+ exit(1);
+ }
+ }
+
+ disk = open(argv[1], part < 0 ? O_RDONLY : O_RDWR);
+ if (disk < 0) {
+ perror(argv[1]);
+ exit(1);
+ }
+
+ nbytes = read(disk, sector, sizeof(sector));
+ if (nbytes != sizeof(sector)) {
+ if ((long) nbytes < 0) {
+ perror("read");
+ } else {
+ fprintf(stderr, "%s: short read\n", prog_name);
+ }
+ exit(1);
+ }
+
+ aboot_pos = sector[61] * 512;
+
+ if (lseek(disk, aboot_pos, SEEK_SET) != aboot_pos) {
+ perror("lseek");
+ exit(1);
+ }
+
+ nbytes = read(disk, sector, sizeof(sector));
+ if (nbytes != sizeof(sector)) {
+ if ((long) nbytes < 0) {
+ perror("read");
+ } else {
+ fprintf(stderr, "%s: short read\n", prog_name);
+ }
+ exit(1);
+ }
+
+ for (i = 0; i < (int) (sizeof(sector)/sizeof(sector[0])); ++i) {
+ if (sector[i] == ABOOT_MAGIC)
+ break;
+ }
+ if (i >= (int) (sizeof(sector)/sizeof(sector[0]))) {
+ fprintf(stderr, "%s: could not find aboot on disk %s\n",
+ prog_name, argv[1]);
+ exit(1);
+ }
+
+ if (part < 0) {
+ printf("aboot.conf partition currently set to %ld\n",
+ sector[i + 1]);
+ exit(0);
+ }
+
+ if (lseek(disk, aboot_pos, SEEK_SET) != aboot_pos) {
+ perror("lseek");
+ exit(1);
+ }
+
+ sector[i + 1] = atoi(argv[2]);
+
+ nbytes = write(disk, sector, sizeof(sector));
+ if (nbytes != sizeof(sector)) {
+ if ((long) nbytes < 0) {
+ perror("write");
+ } else {
+ fprintf(stderr, "%s: short write\n", prog_name);
+ }
+ exit(1);
+ }
+ return 0;
+}
diff --git a/package/aboot/src/tools/bio.c b/package/aboot/src/tools/bio.c
new file mode 100644
index 000000000..c82161e62
--- /dev/null
+++ b/package/aboot/src/tools/bio.c
@@ -0,0 +1,178 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+/* Buffered I/O functions. By cacheing the most recently used blocks,
+ * we can cut WAAY down on disk traffic...
+ */
+
+static int bio_fd = -1;
+static int bio_counter = 0;
+static int bio_blocksize = 0;
+
+struct bio_buf {
+ int blkno;
+ int last_access;
+ int dirty;
+ char * data;
+};
+
+#define NBUFS 32
+struct bio_buf buflist[NBUFS];
+
+/* initialize the buffer cache. Blow away anything that may
+ * have been previously cached...
+ */
+void
+binit(int fd, int blocksize)
+{
+ int i;
+
+ bio_fd = fd;
+ bio_blocksize = blocksize;
+
+ for(i = 0; i < NBUFS; i++) {
+ buflist[i].blkno = 0;
+ if(buflist[i].data) {
+ free(buflist[i].data);
+ }
+ buflist[i].data = 0;
+ buflist[i].last_access = 0;
+ buflist[i].dirty = 0;
+ }
+}
+
+/* Flush out any dirty blocks */
+void
+bflush(void)
+{
+ int i;
+
+ for(i = 0; i < NBUFS; i++) {
+ if(buflist[i].dirty) {
+#ifdef BIO_DEBUG
+ printf("bflush: writing block %d\n", buflist[i].blkno);
+#endif
+ lseek(bio_fd, buflist[i].blkno * bio_blocksize, 0);
+ write(bio_fd, buflist[i].data, bio_blocksize);
+ buflist[i].dirty = 0;
+ }
+ }
+}
+
+/* Read a block. */
+void
+bread(int blkno, void * blkbuf)
+{
+ int i;
+ int lowcount;
+ int lowcount_buf;
+
+ /* First, see if the block is already in memory... */
+ for(i = 0; i < NBUFS; i++) {
+ if(buflist[i].blkno == blkno) {
+ /* Got it! Bump the access count and return. */
+ buflist[i].last_access = ++bio_counter;
+#ifdef BIO_DEBUG
+ printf("bread: buffer hit on block %d\n", blkno);
+#endif
+ memcpy(blkbuf, buflist[i].data, bio_blocksize);
+ return;
+ }
+ }
+
+ /* Not in memory; need to find a buffer and read it in. */
+ lowcount = buflist[0].last_access;
+ lowcount_buf = 0;
+ for(i = 1; i < NBUFS; i++) {
+ if(buflist[i].last_access < lowcount) {
+ lowcount = buflist[i].last_access;
+ lowcount_buf = i;
+ }
+ }
+
+ /* If the buffer is dirty, we need to write it out... */
+ if(buflist[lowcount_buf].dirty) {
+#ifdef BIO_DEBUG
+ printf("bread: recycling dirty buffer %d for block %d\n",
+ lowcount_buf, buflist[lowcount_buf].blkno);
+#endif
+ lseek(bio_fd, buflist[lowcount_buf].blkno * bio_blocksize, 0);
+ write(bio_fd, buflist[lowcount_buf].data, bio_blocksize);
+ buflist[lowcount_buf].dirty = 0;
+ }
+
+#ifdef BIO_DEBUG
+ printf("bread: Using buffer %d for block %d\n", lowcount_buf, blkno);
+#endif
+
+ buflist[lowcount_buf].blkno = blkno;
+ if(!buflist[lowcount_buf].data) {
+ buflist[lowcount_buf].data = (char *)malloc(bio_blocksize);
+ }
+ lseek(bio_fd, blkno * bio_blocksize, 0);
+ if(read(bio_fd,buflist[lowcount_buf].data,bio_blocksize)!=bio_blocksize) {
+ perror("bread: I/O error");
+ }
+
+ buflist[lowcount_buf].last_access = ++bio_counter;
+ memcpy(blkbuf, buflist[lowcount_buf].data, bio_blocksize);
+}
+
+
+/* Write a block */
+void
+bwrite(int blkno, void * blkbuf)
+{
+ int i;
+ int lowcount;
+ int lowcount_buf;
+
+ /* First, see if the block is already in memory... */
+ for(i = 0; i < NBUFS; i++) {
+ if(buflist[i].blkno == blkno) {
+ /* Got it! Bump the access count and return. */
+#ifdef BIO_DEBUG
+ printf("bwrite: buffer hit on block %d\n", blkno);
+#endif
+ buflist[i].last_access = ++bio_counter;
+ memcpy(buflist[i].data, blkbuf, bio_blocksize);
+ buflist[i].dirty = 1;
+ return;
+ }
+ }
+
+ /* Not in memory; need to find a buffer and stuff it. */
+ lowcount = buflist[0].last_access;
+ lowcount_buf = 0;
+ for(i = 1; i < NBUFS; i++) {
+ if(buflist[i].last_access < lowcount) {
+ lowcount = buflist[i].last_access;
+ lowcount_buf = i;
+ }
+ }
+
+ /* If the buffer is dirty, we need to write it out... */
+ if(buflist[lowcount_buf].dirty) {
+#ifdef BIO_DEBUG
+ printf("bwrite: recycling dirty buffer %d for block %d\n",
+ lowcount_buf, buflist[lowcount_buf].blkno);
+#endif
+ lseek(bio_fd, buflist[lowcount_buf].blkno * bio_blocksize, 0);
+ write(bio_fd, buflist[lowcount_buf].data, bio_blocksize);
+ buflist[lowcount_buf].dirty = 0;
+ }
+
+#ifdef BIO_DEBUG
+ printf("bwrite: Using buffer %d for block %d\n", lowcount_buf, blkno);
+#endif
+
+ buflist[lowcount_buf].blkno = blkno;
+ if(!buflist[lowcount_buf].data) {
+ buflist[lowcount_buf].data = (char *)malloc(bio_blocksize);
+ }
+ buflist[lowcount_buf].last_access = ++bio_counter;
+ memcpy(buflist[lowcount_buf].data, blkbuf, bio_blocksize);
+ buflist[lowcount_buf].dirty = 1;
+}
diff --git a/package/aboot/src/tools/bio.h b/package/aboot/src/tools/bio.h
new file mode 100644
index 000000000..398590360
--- /dev/null
+++ b/package/aboot/src/tools/bio.h
@@ -0,0 +1,4 @@
+extern void binit (int fd, int blocksize);
+extern void bflush (void);
+extern void bread (int blkno, void * blkbuf);
+extern void bwrite (int blkno, void * blkbuf);
diff --git a/package/aboot/src/tools/e2lib.c b/package/aboot/src/tools/e2lib.c
new file mode 100644
index 000000000..56e4b66ef
--- /dev/null
+++ b/package/aboot/src/tools/e2lib.c
@@ -0,0 +1,1473 @@
+/* This is a library of functions that allow user-level programs to
+ * read and manipulate ext2 file systems. For convenience sake,
+ * this library maintains a lot of state information in static
+ * variables; therefore, it's not reentrant. We don't care for
+ * our applications 8-)
+ */
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <bio.h>
+#include <e2lib.h>
+
+
+#define MAX_OPEN_FILES 8
+
+int fd = -1;
+struct ext2_super_block sb;
+struct ext2_group_desc *gds;
+int ngroups = 0;
+int blocksize; /* Block size of this fs */
+int directlim; /* Maximum direct blkno */
+int ind1lim; /* Maximum single-indir blkno */
+int ind2lim; /* Maximum double-indir blkno */
+int ptrs_per_blk; /* ptrs/indirect block */
+char filename[256];
+int readonly; /* Is this FS read-only? */
+int verbose = 0;
+int big_endian = 0;
+
+static void ext2_ifree(int ino);
+static void ext2_free_indirect(int indirect_blkno, int level);
+
+
+struct inode_table_entry {
+ struct ext2_inode inode;
+ int inumber;
+ int free;
+ unsigned short old_mode;
+} inode_table[MAX_OPEN_FILES];
+
+/* Utility functions to byte-swap 16 and 32 bit quantities... */
+
+unsigned short
+swap16 (unsigned short s)
+{
+ return((unsigned short)( ((s << 8) & 0xff00) | ((s >> 8) & 0x00ff)));
+}
+
+unsigned int
+swap32 (unsigned int i)
+{
+ return((unsigned int)(
+ ((i << 24) & 0xff000000) |
+ ((i << 8) & 0x00ff0000) |
+ ((i >> 8) & 0x0000ff00) |
+ ((i >> 24) & 0x000000ff)) );
+}
+
+void
+ext2_swap_sb (struct ext2_super_block *sb)
+{
+ sb->s_inodes_count = swap32(sb->s_inodes_count);
+ sb->s_blocks_count = swap32(sb->s_blocks_count);
+ sb->s_r_blocks_count = swap32(sb->s_r_blocks_count);
+ sb->s_free_blocks_count = swap32(sb->s_free_blocks_count);
+ sb->s_free_inodes_count = swap32(sb->s_free_inodes_count);
+ sb->s_first_data_block = swap32(sb->s_first_data_block);
+ sb->s_log_block_size = swap32(sb->s_log_block_size);
+ sb->s_log_frag_size = swap32(sb->s_log_frag_size);
+ sb->s_blocks_per_group = swap32(sb->s_blocks_per_group);
+ sb->s_frags_per_group = swap32(sb->s_frags_per_group);
+ sb->s_inodes_per_group = swap32(sb->s_inodes_per_group);
+ sb->s_mtime = swap32(sb->s_mtime);
+ sb->s_wtime = swap32(sb->s_wtime);
+ sb->s_mnt_count = swap16(sb->s_mnt_count);
+ sb->s_max_mnt_count = swap16(sb->s_max_mnt_count);
+ sb->s_magic = swap16(sb->s_magic);
+ sb->s_state = swap16(sb->s_state);
+ sb->s_errors = swap16(sb->s_errors);
+ sb->s_pad = swap16(sb->s_pad);
+ sb->s_lastcheck = swap32(sb->s_lastcheck);
+ sb->s_checkinterval = swap32(sb->s_checkinterval);
+}
+
+void
+ext2_swap_gd (struct ext2_group_desc *gd)
+{
+ gd->bg_block_bitmap = swap32(gd->bg_block_bitmap);
+ gd->bg_inode_bitmap = swap32(gd->bg_inode_bitmap);
+ gd->bg_inode_table = swap32(gd->bg_inode_table);
+ gd->bg_free_blocks_count = swap16(gd->bg_free_blocks_count);
+ gd->bg_free_inodes_count = swap16(gd->bg_free_inodes_count);
+ gd->bg_used_dirs_count = swap16(gd->bg_used_dirs_count);
+ gd->bg_pad = swap16(gd->bg_pad);
+}
+
+void
+ext2_swap_inode (struct ext2_inode *ip)
+{
+ int i;
+
+ ip->i_mode = swap16(ip->i_mode);
+ ip->i_uid = swap16(ip->i_uid);
+ ip->i_size = swap32(ip->i_size);
+ ip->i_atime = swap32(ip->i_atime);
+ ip->i_ctime = swap32(ip->i_ctime);
+ ip->i_mtime = swap32(ip->i_mtime);
+ ip->i_dtime = swap32(ip->i_dtime);
+ ip->i_gid = swap16(ip->i_gid);
+ ip->i_links_count = swap16(ip->i_links_count);
+ ip->i_blocks = swap32(ip->i_blocks);
+ ip->i_flags = swap32(ip->i_flags);
+ ip->i_reserved1 = swap32(ip->i_reserved1);
+ for(i = 0; i < EXT2_N_BLOCKS; i++) {
+ ip->i_block[i] = swap32(ip->i_block[i]);
+ }
+ ip->i_version = swap32(ip->i_version);
+ ip->i_file_acl = swap32(ip->i_file_acl);
+ ip->i_dir_acl = swap32(ip->i_dir_acl);
+ ip->i_faddr = swap32(ip->i_faddr);
+ ip->i_pad1 = swap16(ip->i_pad1);
+}
+
+
+
+/* Initialize an ext2 filesystem; this is sort-of the same idea as
+ * "mounting" it. Read in the relevant control structures and
+ * make them available to the user. Returns 0 if successful, -1 on
+ * failure.
+ */
+int
+ext2_init (char * name, int access)
+{
+ int i;
+
+ /* Initialize the inode table */
+ for(i = 0; i < MAX_OPEN_FILES; i++) {
+ inode_table[i].free = 1;
+ inode_table[i].inumber = 0;
+ }
+
+ if((access != O_RDONLY) && (access != O_RDWR)) {
+ fprintf(stderr,
+ "ext2_init: Access must be O_RDONLY or O_RDWR, not %d\n",
+ access);
+ return(-1);
+ }
+
+ /* Open the device/file */
+ fd = open(name, access);
+ if(fd < 0) {
+ perror(filename);
+ return(-1);
+ }
+
+ if(access == O_RDONLY) {
+ readonly = 1;
+ }
+
+ /* Read in the first superblock */
+ lseek(fd, EXT2_MIN_BLOCK_SIZE, SEEK_SET);
+ if(read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
+ perror("ext2 sb read");
+ close(fd);
+ return(-1);
+ }
+
+ if((sb.s_magic != EXT2_SUPER_MAGIC) && (sb.s_magic != EXT2_SUPER_BIGMAGIC)) {
+ fprintf(stderr, "ext2 bad magic 0x%x\n", sb.s_magic);
+ close(fd);
+ return(-1);
+ }
+
+ if(sb.s_magic == EXT2_SUPER_BIGMAGIC) {
+ big_endian = 1;
+
+ /* Byte-swap the fields in the superblock... */
+ ext2_swap_sb(&sb);
+ }
+
+ if(sb.s_first_data_block != 1) {
+ fprintf(stderr,
+ "Brain-damaged utils can't deal with a filesystem\nwhere s_first_data_block != 1.\nRe-initialize the filesystem\n");
+ close(fd);
+ return(-1);
+ }
+
+ ngroups = (sb.s_blocks_count+sb.s_blocks_per_group-1)/sb.s_blocks_per_group;
+ gds = (struct ext2_group_desc *)
+ malloc((size_t)(ngroups * sizeof(struct ext2_group_desc)));
+
+ /* Read in the group descriptors (immediately follows superblock) */
+ if ((size_t) read(fd, gds, ngroups * sizeof(struct ext2_group_desc))
+ != (ngroups * sizeof(struct ext2_group_desc)))
+ {
+ perror("ext2_init: group desc read error");
+ return(-1);
+ }
+
+ if(big_endian) {
+ for(i = 0; i < ngroups; i++) {
+ ext2_swap_gd(&(gds[i]));
+ }
+ }
+
+ strcpy(filename, name);
+
+ /* Calculate direct/indirect block limits for this file system
+ * (blocksize dependent)
+ */
+ blocksize = EXT2_BLOCK_SIZE(&sb);
+ directlim = EXT2_NDIR_BLOCKS - 1;
+ ptrs_per_blk = blocksize/sizeof(unsigned int);
+ ind1lim = ptrs_per_blk + directlim;
+ ind2lim = (ptrs_per_blk * ptrs_per_blk) + directlim;
+
+ if(getenv("EXT2_VERBOSE")) {
+ verbose = 1;
+ }
+
+ binit(fd, blocksize);
+
+ if(verbose) {
+ printf("Initialized filesystem %s\n", filename);
+ printf(" %d blocks (%dKb), %d free (%dKb)\n",
+ sb.s_blocks_count, (sb.s_blocks_count * blocksize)/1024,
+ sb.s_free_blocks_count,
+ (sb.s_free_blocks_count * blocksize)/1024);
+ printf(" %d inodes, %d free\n",
+ sb.s_inodes_count, sb.s_free_inodes_count);
+ printf(" %d groups, %d blocks/group\n",
+ ngroups, sb.s_blocks_per_group);
+ }
+
+ return(0);
+}
+
+int
+ext2_blocksize (void)
+{
+ return blocksize;
+}
+
+int
+ext2_total_blocks (void)
+{
+ return sb.s_blocks_count;
+}
+
+int
+ext2_free_blocks (void)
+{
+ return sb.s_free_blocks_count;
+}
+
+int
+ext2_total_inodes (void)
+{
+ return sb.s_inodes_count;
+}
+
+int
+ext2_free_inodes (void)
+{
+ return sb.s_free_inodes_count;
+}
+
+/* Call this when we're all done with the file system. This will write
+ * back any superblock and group changes to the file system.
+ */
+void
+ext2_close (void)
+{
+ int i;
+ int errors = 0;
+ int blocks_per_group = sb.s_blocks_per_group;
+
+ if(!readonly) {
+
+ if(big_endian) {
+ ext2_swap_sb(&sb);
+ for(i = 0; i < ngroups; i++) {
+ ext2_swap_gd(&(gds[i]));
+ }
+ }
+
+ for(i = 0; i < ngroups; i++) {
+ lseek(fd, ((i*blocks_per_group)+1)*blocksize, SEEK_SET);
+ if(write(fd, &sb, sizeof(sb)) != sizeof(sb)) {
+ perror("sb write");
+ errors = 1;
+ }
+
+ if ((size_t) write(fd, gds, ngroups*sizeof(struct ext2_group_desc))
+ != ngroups*sizeof(struct ext2_group_desc))
+ {
+ perror("gds write");
+ errors = 1;
+ }
+
+ bflush();
+ }
+ }
+
+ close(fd);
+ if(errors) {
+ fprintf(stderr, "Errors encountered while updating %s\n", filename);
+ fprintf(stderr, "e2fsck is STRONGLY recommended!\n");
+ }
+}
+
+
+
+/* Read the specified inode from the disk and return it to the user.
+ * Returns NULL if the inode can't be read...
+ */
+struct ext2_inode *
+ext2_iget (int ino)
+{
+ int i;
+ struct ext2_inode * ip = NULL;
+ struct inode_table_entry * itp = NULL;
+ int group;
+ int blkoffset;
+ int byteoffset;
+ char inobuf[EXT2_MAX_BLOCK_SIZE];
+
+ for(i = 0; i < MAX_OPEN_FILES; i++) {
+ if(inode_table[i].free) {
+ itp = &(inode_table[i]);
+ ip = &(itp->inode);
+ break;
+ }
+ }
+ if(!ip) {
+ fprintf(stderr, "ext2_iget: no free inodes\n");
+ return(NULL);
+ }
+
+ group = ino / sb.s_inodes_per_group;
+ blkoffset = (gds[group].bg_inode_table * blocksize);
+ byteoffset = ((ino-1) % sb.s_inodes_per_group) * sizeof(struct ext2_inode);
+ blkoffset += ((byteoffset / blocksize) * blocksize);
+ byteoffset = (byteoffset % blocksize);
+ bread(blkoffset/blocksize, inobuf);
+
+ memcpy(ip, &(inobuf[byteoffset]), sizeof(struct ext2_inode));
+
+ if(big_endian) {
+ ext2_swap_inode(ip);
+ }
+
+ /* Yes, this is ugly, but it makes iput SOOO much easier 8-) */
+ itp->free = 0;
+ itp->inumber = ino;
+ itp->old_mode = ip->i_mode;
+
+ return(ip);
+}
+
+/* Put the specified inode back on the disk where it came from. */
+void
+ext2_iput (struct ext2_inode *ip)
+{
+ int group;
+ int blkoffset;
+ int byteoffset;
+ int ino;
+ struct inode_table_entry *itp;
+ char inobuf[EXT2_MAX_BLOCK_SIZE];
+ int inode_mode;
+
+ itp = (struct inode_table_entry *)ip;
+ ino = itp->inumber;
+
+ if(ip->i_links_count == 0) {
+ ext2_ifree(itp->inumber);
+ }
+
+ itp->inumber = 0;
+
+ if(!readonly) {
+ group = ino / sb.s_inodes_per_group;
+ blkoffset = (gds[group].bg_inode_table * blocksize);
+ byteoffset = ((ino-1) % sb.s_inodes_per_group) * sizeof(struct ext2_inode);
+ blkoffset += (byteoffset / blocksize) * blocksize;
+ byteoffset = byteoffset % blocksize;
+
+ inode_mode = ip->i_mode;
+ bread(blkoffset/blocksize, inobuf);
+ if(big_endian) {
+ ext2_swap_inode(ip);
+ }
+ memcpy(&(inobuf[byteoffset]), ip, sizeof(struct ext2_inode));
+ bwrite(blkoffset/blocksize, inobuf);
+
+ if(S_ISDIR(itp->old_mode) && !S_ISDIR(inode_mode)) {
+ /* We deleted a directory */
+ gds[group].bg_used_dirs_count--;
+ }
+ if(!S_ISDIR(itp->old_mode) && S_ISDIR(inode_mode)) {
+ /* We created a directory */
+ gds[group].bg_used_dirs_count++;
+ }
+ }
+
+ itp->free = 1;
+}
+
+#define BITS_PER_LONG (8*sizeof(int))
+
+static int
+find_first_zero_bit (unsigned int * addr, unsigned size)
+{
+ unsigned lwsize;
+ unsigned int *ap = (unsigned int *)addr;
+ unsigned int mask;
+ unsigned int longword, bit;
+ unsigned int lwval;
+
+ if (!size)
+ return 0;
+
+ /* Convert "size" to a whole number of longwords... */
+ lwsize = (size + BITS_PER_LONG - 1) >> 5;
+ for (longword = 0; longword < lwsize; longword++, ap++) {
+ if(*ap != 0xffffffff) {
+ lwval = big_endian ? swap32(*ap) : *ap;
+
+ for (bit = 0, mask = 1; bit < BITS_PER_LONG; bit++, mask <<= 1)
+ {
+ if ((lwval & mask) == 0) {
+ return (longword*BITS_PER_LONG) + bit;
+ }
+ }
+ }
+ }
+ return size;
+
+}
+
+static void
+set_bit (unsigned int *addr, int bitno)
+{
+ if(big_endian) {
+ int lwval;
+ lwval = swap32(addr[bitno/BITS_PER_LONG]);
+ lwval |= (1 << (bitno % BITS_PER_LONG));
+ addr[bitno/BITS_PER_LONG] = swap32(lwval);
+ }
+ else {
+ addr[bitno / BITS_PER_LONG] |= (1 << (bitno % BITS_PER_LONG));
+ }
+}
+
+static void
+clear_bit (unsigned int *addr, int bitno)
+{
+ if(big_endian) {
+ int lwval;
+ lwval = swap32(addr[bitno/BITS_PER_LONG]);
+ lwval &= ~((unsigned int)(1 << (bitno % BITS_PER_LONG)));
+ addr[bitno/BITS_PER_LONG] = swap32(lwval);
+ }
+ else {
+ addr[bitno / BITS_PER_LONG] &=
+ ~((unsigned int)(1 << (bitno % BITS_PER_LONG)));
+ }
+}
+
+
+/* Allocate a block from the file system. Brain-damaged implementation;
+ * doesn't even TRY to do load-balancing among groups... just grabs the
+ * first block it can find...
+ */
+int
+ext2_balloc (void)
+{
+ unsigned int blk, blockmap[256];
+ int i;
+
+ if(readonly) {
+ fprintf(stderr, "ext2_balloc: readonly filesystem\n");
+ return(0);
+ }
+
+ for(i = 0; i < ngroups; i++) {
+ if(gds[i].bg_free_blocks_count > 0) {
+ bread(gds[i].bg_block_bitmap, blockmap);
+ blk = find_first_zero_bit(blockmap, sb.s_blocks_per_group);
+ if (blk == 0 || blk == sb.s_blocks_per_group) {
+ fprintf(stderr,
+ "group %d has %d blocks free but none in bitmap?\n",
+ i, gds[i].bg_free_blocks_count);
+ continue;
+ }
+ set_bit(blockmap, blk);
+ bwrite(gds[i].bg_block_bitmap, blockmap);
+ gds[i].bg_free_blocks_count--;
+ sb.s_free_blocks_count--;
+ blk = blk + (i*sb.s_blocks_per_group)+1;
+
+ if(blk == 0) {
+ fprintf(stderr, "ext2_balloc: blk == 0?\n");
+ }
+ return(blk);
+ }
+ }
+
+ if(verbose) {
+ printf("ext2_balloc: can't find a free block\n");
+ }
+ return(0);
+}
+
+/* Deallocate a block */
+void
+ext2_bfree (int blk)
+{
+ int i;
+ unsigned int blockmap[256];
+
+ /* Find which group this block is in */
+ i = (blk-1) / sb.s_blocks_per_group;
+
+ /* Read the block map */
+ bread(gds[i].bg_block_bitmap, blockmap);
+
+ /* Clear the appropriate bit */
+ clear_bit(blockmap, (blk-1) % sb.s_blocks_per_group);
+
+ /* Write the block map back out */
+ bwrite(gds[i].bg_block_bitmap, blockmap);
+
+ /* Update free block counts. */
+ gds[i].bg_free_blocks_count++;
+ sb.s_free_blocks_count++;
+
+}
+
+/* Allocate a contiguous range of blocks. This is used ONLY for
+ * initializing the bootstrapper. It uses a simple-minded algorithm
+ * that works best on a clean or nearly clean file system... we
+ * chunk through the bitmap a longword at a time. Only if the whole
+ * longword indicates free blocks do we use it. On a 32-bit system,
+ * this means we allocate blocks only in units of 32.
+ */
+int
+ext2_contiguous_balloc (int nblocks)
+{
+ int i, j;
+ int firstlong, lastlong;
+ int longs_needed;
+ int longs_per_group;
+ int blk;
+ unsigned int blockmap[256];
+
+ if(readonly) {
+ fprintf(stderr, "ext2_contiguous_balloc: readonly filesystem\n");
+ return(0);
+ }
+
+ /* Compute how many longwords we need to fulfill this request */
+ longs_needed = (nblocks + BITS_PER_LONG - 1) / BITS_PER_LONG;
+ longs_per_group = sb.s_blocks_per_group/BITS_PER_LONG;
+
+ for(i = 0; i < ngroups; i++) {
+ /* Don't even bother if this group doesn't have enough blocks! */
+ if(gds[i].bg_free_blocks_count >= nblocks) {
+
+ /* Get the block map. */
+ bread(gds[i].bg_block_bitmap, blockmap);
+
+ /* Find a run of blocks */
+ firstlong = 0;
+
+ do {
+ for(; firstlong < longs_per_group; firstlong++) {
+ if(blockmap[firstlong] == 0) break;
+ }
+
+ if(firstlong == longs_per_group) {
+ /* No such thing in this group; try another! */
+ break;
+ }
+
+ for(lastlong = firstlong; lastlong < longs_per_group;
+ lastlong++) {
+ if(blockmap[lastlong] != 0) break;
+ }
+
+ if((lastlong-firstlong) < longs_needed) {
+ firstlong = lastlong;
+ }
+ } while((lastlong-firstlong) < longs_needed);
+
+ /* If we got all the way through the block map,
+ * try another group.
+ */
+ if(firstlong == longs_per_group) {
+ continue;
+ }
+
+ /* If we get here, then we know that we have a run
+ * that will fit our allocation. Allocate the *actual*
+ * blocks that we need!
+ */
+ blk = firstlong * BITS_PER_LONG;
+ for(j = 0; j < nblocks; j++) {
+ set_bit(blockmap, blk+j);
+ }
+
+ bwrite(gds[i].bg_block_bitmap, blockmap);
+ gds[i].bg_free_blocks_count -= nblocks;
+ sb.s_free_blocks_count -= nblocks;
+ blk = blk + (i*sb.s_blocks_per_group)+1;
+
+ if(verbose) {
+ printf("ext2_contiguous_balloc: allocated %d blks @%d\n",
+ nblocks, blk);
+ }
+ return(blk);
+ }
+ }
+
+ if(verbose) {
+ printf("ext2_contiguous_balloc: can't find %d contiguous free blocks\n", nblocks);
+ }
+ return(0);
+}
+
+
+/* Pre-allocate contiguous blocks to the specified inode. Note that the
+ * DATA blocks must be contiguous; indirect blocks can come from anywhere.
+ * This is for the benefit of the bootstrap loader.
+ * If successful, this routine returns the block number of the first
+ * data block of the file. Otherwise, it returns -1.
+ */
+int
+ext2_fill_contiguous (struct ext2_inode * ip, int nblocks)
+{
+ int iblkno = 0;
+ int firstblock;
+ int i;
+ unsigned int *lp = NULL;
+ char blkbuf[EXT2_MAX_BLOCK_SIZE];
+
+ /* For simplicity's sake, we only allow single indirection
+ * here. We shouldn't need more than this anyway!
+ */
+ if(nblocks > ind1lim) {
+ fprintf(stderr,
+ "ext2_fill_contiguous: file size too big (%d); cannot exceed %d\n",
+ nblocks, ind1lim);
+ return(-1);
+ }
+
+ /* First, try to allocate the data blocks */
+ firstblock = ext2_contiguous_balloc(nblocks);
+
+ if(firstblock == 0) {
+ fprintf(stderr,
+ "ext2_fill_contiguous: Cannot allocate %d contiguous blocks\n", nblocks);
+ return(-1);
+ }
+
+ ip->i_blocks = nblocks * (blocksize/512);
+
+ /* If we need the indirect block, then allocate it now. */
+ if(nblocks > directlim) {
+ iblkno = ext2_balloc();
+ if(iblkno == 0) {
+ /* Should rarely happen! */
+ fprintf(stderr,
+ "ext2_fill_contiguous: cannot allocate indirect block\n");
+ for(i = 0; i < nblocks; i++) {
+ ext2_bfree(i);
+ }
+ return(-1);
+ }
+ ip->i_blocks += (blocksize/512);
+ /* Point to indirect block buffer, in case we need it! */
+ lp = (unsigned int *)blkbuf;
+
+ for(i = 0; i < ptrs_per_blk; i++) {
+ lp[i] = 0;
+ }
+
+ ip->i_block[EXT2_IND_BLOCK] = iblkno;
+ }
+
+ /* All set... let's roll! */
+ ip->i_size = nblocks * blocksize;
+
+ for(i = 0; i < nblocks; i++) {
+ if(i < EXT2_NDIR_BLOCKS) {
+ ip->i_block[i] = firstblock+i;
+ }
+ else {
+ *lp++ = big_endian ? swap32(firstblock+i) : firstblock+i;
+ }
+ }
+
+ /* Write back the indirect block if necessary... */
+ if(iblkno) {
+ bwrite(iblkno, blkbuf);
+ }
+
+ return(firstblock);
+}
+
+/* Write out a boot block for this file system. The caller
+ * should have instantiated the block.
+ */
+void
+ext2_write_bootblock (char *bb)
+{
+ bwrite(0, bb);
+}
+
+
+/* Allocate an inode from the file system. Brain-damaged implementation;
+ * doesn't even TRY to do load-balancing among groups... just grabs the
+ * first inode it can find...
+ */
+int
+ext2_ialloc (void)
+{
+ unsigned int inodemap[256];
+ int i, ino;
+
+ if(readonly) {
+ return(0);
+ }
+ for(i = 0; i < ngroups; i++) {