📄 rd.c
字号:
fp->f_op->read(fp->f_inode, fp, buf, size); /* * If it matches the gzip magic numbers, return -1 */ if (buf[0] == 037 && ((buf[1] == 0213) || (buf[1] == 0236))) { printk(KERN_NOTICE "RAMDISK: Compressed image found at block %d\n", start_block); nblocks = 0; goto done; } /* * Read block 1 to test for minix and ext2 superblock */ if (fp->f_op->lseek) fp->f_op->lseek(fp->f_inode, fp, (start_block+1) * BLOCK_SIZE, 0); fp->f_pos = (start_block+1) * BLOCK_SIZE; fp->f_op->read(fp->f_inode, fp, buf, size); /* Try minix */ if (minixsb->s_magic == MINIX_SUPER_MAGIC || minixsb->s_magic == MINIX_SUPER_MAGIC2) { printk(KERN_NOTICE "RAMDISK: Minix filesystem found at block %d\n", start_block); nblocks = minixsb->s_nzones << minixsb->s_log_zone_size; goto done; } /* Try ext2 */ if (ext2sb->s_magic == EXT2_SUPER_MAGIC) { printk(KERN_NOTICE "RAMDISK: Ext2 filesystem found at block %d\n", start_block); nblocks = ext2sb->s_blocks_count; goto done; } printk(KERN_NOTICE "RAMDISK: Couldn't find valid ramdisk image starting at %d.\n", start_block); done: if (fp->f_op->lseek) fp->f_op->lseek(fp->f_inode, fp, start_block * BLOCK_SIZE, 0); fp->f_pos = start_block * BLOCK_SIZE; if ((nblocks > 0) && blk_size[MAJOR(device)]) { max_blocks = blk_size[MAJOR(device)][MINOR(device)]; max_blocks -= start_block; if (nblocks > max_blocks) { printk(KERN_NOTICE "RAMDISK: Restricting filesystem size " "from %d to %d blocks.\n", nblocks, max_blocks); nblocks = max_blocks; } } kfree(buf); return nblocks;}/* * This routine loads in the ramdisk image. */static void rd_load_image(kdev_t device,int offset, int unit){ struct inode inode, out_inode; struct file infile, outfile; unsigned short fs; kdev_t ram_device; int nblocks, i; char *buf; unsigned short rotate = 0; char rotator[4] = { '|' , '/' , '-' , '\\' }; ram_device = MKDEV(MAJOR_NR, unit); memset(&infile, 0, sizeof(infile)); memset(&inode, 0, sizeof(inode)); inode.i_rdev = device; infile.f_mode = 1; /* read only */ infile.f_inode = &inode; memset(&outfile, 0, sizeof(outfile)); memset(&out_inode, 0, sizeof(out_inode)); out_inode.i_rdev = ram_device; outfile.f_mode = 3; /* read/write */ outfile.f_inode = &out_inode; if (blkdev_open(&inode, &infile) != 0) return; if (blkdev_open(&out_inode, &outfile) != 0) return; fs = get_fs(); set_fs(KERNEL_DS); nblocks = identify_ramdisk_image(device, &infile, offset); if (nblocks < 0) goto done; if (nblocks == 0) {#ifdef BUILD_CRAMDISK if (crd_load(&infile, &outfile) == 0) goto successful_load;#else printk(KERN_NOTICE "RAMDISK: Kernel does not support compressed " "ramdisk images\n");#endif goto done; } if (nblocks > (rd_length[0] >> BLOCK_SIZE_BITS)) { printk("RAMDISK: image too big! (%d/%d blocks)\n", nblocks, rd_length[0] >> BLOCK_SIZE_BITS); goto done; } /* * OK, time to copy in the data */ buf = kmalloc(BLOCK_SIZE, GFP_KERNEL); if (buf == 0) { printk(KERN_ERR "RAMDISK: could not allocate buffer\n"); goto done; } printk(KERN_NOTICE "RAMDISK: Loading %d blocks into ram disk... ", nblocks); for (i=0; i < nblocks; i++) { infile.f_op->read(infile.f_inode, &infile, buf, BLOCK_SIZE); outfile.f_op->write(outfile.f_inode, &outfile, buf, BLOCK_SIZE); if (!(i % 16)) { printk("%c\b", rotator[rotate & 0x3]); rotate++; } } printk("done.\n"); kfree(buf);successful_load: invalidate_buffers(device); ROOT_DEV = MKDEV(MAJOR_NR,unit);done: if (infile.f_op->release) infile.f_op->release(&inode, &infile); set_fs(fs);}static void rd_load_disk(int n){#ifdef CONFIG_BLK_DEV_INITRD extern kdev_t real_root_dev;#endif if (rd_doload == 0) return; if (MAJOR(ROOT_DEV) != FLOPPY_MAJOR#ifdef CONFIG_BLK_DEV_INITRD && MAJOR(real_root_dev) != FLOPPY_MAJOR#endif ) return; if (rd_prompt) {#ifdef CONFIG_BLK_DEV_FD floppy_eject();#endif printk(KERN_NOTICE "VFS: Insert root floppy disk to be loaded into ramdisk and press ENTER\n"); wait_for_keypress(); } rd_load_image(ROOT_DEV,rd_image_start,n);}void rd_load(void){ rd_load_disk(0);}void rd_load_secondary(void){ rd_load_disk(1);}#ifdef CONFIG_BLK_DEV_INITRDvoid initrd_load(void){ rd_load_image(MKDEV(MAJOR_NR, INITRD_MINOR),0,0);}#endif#endif /* RD_LOADER */#ifdef BUILD_CRAMDISK/* * gzip declarations */#define OF(args) args#define memzero(s, n) memset ((s), 0, (n))typedef unsigned char uch;typedef unsigned short ush;typedef unsigned long ulg;#define INBUFSIZ 4096#define WSIZE 0x8000 /* window size--must be a power of two, and */ /* at least 32K for zip's deflate method */static uch *inbuf;static uch *window;static unsigned insize = 0; /* valid bytes in inbuf */static unsigned inptr = 0; /* index of next byte to be processed in inbuf */static unsigned outcnt = 0; /* bytes in output buffer */static int exit_code = 0;static long bytes_out = 0;static struct file *crd_infp, *crd_outfp;#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf()) /* Diagnostic functions (stubbed out) */#define Assert(cond,msg)#define Trace(x)#define Tracev(x)#define Tracevv(x)#define Tracec(c,x)#define Tracecv(c,x)#define STATIC staticstatic int fill_inbuf(void);static void flush_window(void);static void *malloc(int size);static void free(void *where);static void error(char *m);static void gzip_mark(void **);static void gzip_release(void **);#include "../../../../lib/inflate.c"static void *malloc(int size){ return kmalloc(size, GFP_KERNEL);}static void free(void *where){ kfree(where);}static void gzip_mark(void **ptr){}static void gzip_release(void **ptr){}/* =========================================================================== * Fill the input buffer. This is called only when the buffer is empty * and at least one byte is really needed. */static int fill_inbuf(){ if (exit_code) return -1; insize = crd_infp->f_op->read(crd_infp->f_inode, crd_infp, inbuf, INBUFSIZ); if (insize == 0) return -1; inptr = 1; return inbuf[0];}/* =========================================================================== * Write the output window window[0..outcnt-1] and update crc and bytes_out. * (Used for the decompressed data only.) */static void flush_window(){ ulg c = crc; /* temporary variable */ unsigned n; uch *in, ch; crd_outfp->f_op->write(crd_outfp->f_inode, crd_outfp, window, outcnt); in = window; for (n = 0; n < outcnt; n++) { ch = *in++; c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8); } crc = c; bytes_out += (ulg)outcnt; outcnt = 0;}static void error(char *x){ printk(KERN_ERR "%s", x); exit_code = 1;}static intcrd_load(struct file * fp, struct file *outfp){ int result; insize = 0; /* valid bytes in inbuf */ inptr = 0; /* index of next byte to be processed in inbuf */ outcnt = 0; /* bytes in output buffer */ exit_code = 0; bytes_out = 0; crc = 0xFFFFFFFF; crd_infp = fp; crd_outfp = outfp; inbuf = kmalloc(INBUFSIZ, GFP_KERNEL); if (inbuf == 0) { printk(KERN_ERR "RAMDISK: Couldn't allocate gzip buffer\n"); return -1; } window = kmalloc(WSIZE, GFP_KERNEL); if (window == 0) { printk(KERN_ERR "RAMDISK: Couldn't allocate gzip window\n"); kfree(inbuf); return -1; } makecrc(); result = gunzip(); kfree(inbuf); kfree(window); return result;}#endif /* BUILD_CRAMDISK */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -