OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [armnommu/] [drivers/] [block/] [rd.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1622 jcastillo
/*
2
 * ramdisk.c - Multiple ramdisk driver - gzip-loading version - v. 0.8 beta.
3
 *
4
 * (C) Chad Page, Theodore Ts'o, et. al, 1995.
5
 *
6
 * This ramdisk is designed to have filesystems created on it and mounted
7
 * just like a regular floppy disk.
8
 *
9
 * It also does something suggested by Linus: use the buffer cache as the
10
 * ramdisk data.  This makes it possible to dynamically allocate the ramdisk
11
 * buffer - with some consequences I have to deal with as I write this.
12
 *
13
 * This code is based on the original ramdisk.c, written mostly by
14
 * Theodore Ts'o (TYT) in 1991.  The code was largely rewritten by
15
 * Chad Page to use the buffer cache to store the ramdisk data in
16
 * 1995; Theodore then took over the driver again, and cleaned it up
17
 * for inclusion in the mainline kernel.
18
 *
19
 * The original CRAMDISK code was written by Richard Lyons, and
20
 * adapted by Chad Page to use the new ramdisk interface.  Theodore
21
 * Ts'o rewrote it so that both the compressed ramdisk loader and the
22
 * kernel decompressor uses the same inflate.c codebase.  The ramdisk
23
 * loader now also loads into a dynamic (buffer cache based) ramdisk,
24
 * not the old static ramdisk.  Support for the old static ramdisk has
25
 * been completely removed.
26
 *
27
 * Loadable module support added by Tom Dyas.
28
 *
29
 * Further cleanups by Chad Page (page0588@sundance.sjsu.edu):
30
 *      Cosmetic changes in #ifdef MODULE, code movement, etc...
31
 *      When the ramdisk is rmmod'ed, free the protected buffers
32
 *      Default ramdisk size changed to 2.88MB
33
 *
34
 *  Added initrd: Werner Almesberger & Hans Lermen, Feb '96
35
 *
36
 * 4/25/96 : Made ramdisk size a parameter (default is now 4MB)
37
 *              - Chad Page
38
 */
39
 
40
#include <linux/config.h>
41
#include <linux/sched.h>
42
#include <linux/minix_fs.h>
43
#include <linux/ext2_fs.h>
44
#include <linux/fs.h>
45
#include <linux/kernel.h>
46
#include <linux/string.h>
47
#include <linux/mm.h>
48
#include <linux/mman.h>
49
#include <linux/malloc.h>
50
#include <linux/ioctl.h>
51
#include <linux/fd.h>
52
#include <linux/module.h>
53
 
54
#include <asm/system.h>
55
#include <asm/segment.h>
56
 
57
extern void wait_for_keypress(void);
58
 
59
/*
60
 * 35 has been officially registered as the RAMDISK major number, but
61
 * so is the original MAJOR number of 1.  We're using 1 in
62
 * include/linux/major.h for now
63
 */
64
#define MAJOR_NR RAMDISK_MAJOR
65
#include <linux/blk.h>
66
 
67
/* The ramdisk size is now a parameter */
68
#define NUM_RAMDISKS 16         /* This cannot be overridden (yet) */ 
69
 
70
#ifndef MODULE
71
/* We don't have to load ramdisks or gunzip them in a module... */
72
#define RD_LOADER
73
#define BUILD_CRAMDISK
74
 
75
void rd_load(void);
76
static int crd_load(struct file *fp, struct file *outfp);
77
 
78
#ifdef CONFIG_BLK_DEV_INITRD
79
static int initrd_users = 0;
80
#endif
81
#endif
82
 
83
/* Various static variables go here... mostly used within the ramdisk code only. */
84
 
85
static int rd_length[NUM_RAMDISKS];
86
static int rd_blocksizes[NUM_RAMDISKS];
87
 
88
/*
89
 * Parameters for the boot-loading of the ramdisk.  These are set by
90
 * init/main.c (from arguments to the kernel command line) or from the
91
 * architecture-specific setup routine (from the stored bootsector
92
 * information).
93
 */
94
int rd_size = 4096;             /* Size of the ramdisks */
95
 
96
#ifndef MODULE
97
int rd_doload = 0;               /* 1 = load ramdisk, 0 = don't load */
98
int rd_prompt = 1;              /* 1 = prompt for ramdisk, 0 = don't prompt */
99
int rd_image_start = 0;          /* starting block # of image */
100
#ifdef CONFIG_BLK_DEV_INITRD
101
unsigned long initrd_start,initrd_end;
102
int mount_initrd = 1;           /* zero if initrd should not be mounted */
103
#endif
104
#endif
105
 
106
/*
107
 *  Basically, my strategy here is to set up a buffer-head which can't be
108
 *  deleted, and make that my Ramdisk.  If the request is outside of the
109
 *  allocated size, we must get rid of it...
110
 *
111
 */
112
static void rd_request(void)
113
{
114
        unsigned int minor;
115
        int offset, len;
116
 
117
repeat:
118
        INIT_REQUEST;
119
 
120
        minor = MINOR(CURRENT->rq_dev);
121
 
122
        if (minor >= NUM_RAMDISKS) {
123
                end_request(0);
124
                goto repeat;
125
        }
126
 
127
        offset = CURRENT->sector << 9;
128
        len = CURRENT->current_nr_sectors << 9;
129
 
130
        if ((offset + len) > rd_length[minor]) {
131
                end_request(0);
132
                goto repeat;
133
        }
134
 
135
        /*
136
         * If we're reading, fill the buffer with 0's.  This is okay since
137
         * we're using protected buffers which should never get freed...
138
         *
139
         * If we're writing, we protect the buffer.
140
         */
141
 
142
        if (CURRENT->cmd == READ)
143
                memset(CURRENT->buffer, 0, len);
144
        else
145
                set_bit(BH_Protected, &CURRENT->bh->b_state);
146
 
147
        end_request(1);
148
        goto repeat;
149
}
150
 
151
static int rd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
152
{
153
        int err;
154
 
155
        if (!inode || !inode->i_rdev)
156
                return -EINVAL;
157
 
158
        switch (cmd) {
159
                case BLKFLSBUF:
160
                        if (!suser()) return -EACCES;
161
                        invalidate_buffers(inode->i_rdev);
162
                        break;
163
                case BLKGETSIZE:   /* Return device size */
164
                        if (!arg)  return -EINVAL;
165
                        err = verify_area(VERIFY_WRITE, (long *) arg,
166
                                          sizeof(long));
167
                        if (err)
168
                                return err;
169
                        put_user(rd_length[MINOR(inode->i_rdev)] / 512,
170
                                 (long *) arg);
171
                        return 0;
172
 
173
                default:
174
                        break;
175
        };
176
 
177
        return 0;
178
}
179
 
180
 
181
#ifdef CONFIG_BLK_DEV_INITRD
182
 
183
static int initrd_read(struct inode *inode,struct file *file,char *buf,
184
    int count)
185
{
186
        int left;
187
 
188
        left = initrd_end-initrd_start-file->f_pos;
189
        if (count > left) count = left;
190
        if (count <= 0) return 0;
191
        memcpy_tofs(buf,(char *) initrd_start+file->f_pos,count);
192
        file->f_pos += count;
193
        return count;
194
}
195
 
196
 
197
static void initrd_release(struct inode *inode,struct file *file)
198
{
199
        unsigned long i;
200
 
201
        if (--initrd_users) return;
202
        for (i = initrd_start; i < initrd_end; i += PAGE_SIZE)
203
                free_page(i);
204
        initrd_start = 0;
205
}
206
 
207
 
208
static struct file_operations initrd_fops = {
209
        NULL,           /* lseek */
210
        initrd_read,    /* read */
211
        NULL,           /* write */
212
        NULL,           /* readdir */
213
        NULL,           /* select */
214
        NULL,           /* ioctl */
215
        NULL,           /* mmap */
216
        NULL,           /* open */
217
        initrd_release, /* release */
218
        NULL            /* fsync */
219
};
220
 
221
#endif
222
 
223
 
224
static int rd_open(struct inode * inode, struct file * filp)
225
{
226
#ifdef CONFIG_BLK_DEV_INITRD
227
        if (DEVICE_NR(inode->i_rdev) == INITRD_MINOR) {
228
                if (!initrd_start) return -ENODEV;
229
                initrd_users++;
230
                filp->f_op = &initrd_fops;
231
                return 0;
232
        }
233
#endif
234
 
235
        if (DEVICE_NR(inode->i_rdev) >= NUM_RAMDISKS)
236
                return -ENXIO;
237
 
238
        MOD_INC_USE_COUNT;
239
 
240
        return 0;
241
}
242
 
243
#ifdef MODULE
244
static void rd_release(struct inode * inode, struct file * filp)
245
{
246
        MOD_DEC_USE_COUNT;
247
}
248
#endif
249
 
250
static struct file_operations fd_fops = {
251
        NULL,           /* lseek - default */
252
        block_read,     /* read - block dev read */
253
        block_write,    /* write - block dev write */
254
        NULL,           /* readdir - not here! */
255
        NULL,           /* select */
256
        rd_ioctl,       /* ioctl */
257
        NULL,           /* mmap */
258
        rd_open,        /* open */
259
#ifndef MODULE
260
        NULL,           /* no special release code... */
261
#else
262
        rd_release,     /* module needs to decrement use count */
263
#endif
264
        block_fsync             /* fsync */
265
};
266
 
267
/* This is the registration and initialization section of the ramdisk driver */
268
int rd_init(void)
269
{
270
        int             i;
271
 
272
        if (register_blkdev(MAJOR_NR, "ramdisk", &fd_fops)) {
273
                printk("RAMDISK: Could not get major %d", MAJOR_NR);
274
                return -EIO;
275
        }
276
 
277
        blk_dev[MAJOR_NR].request_fn = &rd_request;
278
 
279
        for (i = 0; i < NUM_RAMDISKS; i++) {
280
                rd_length[i] = (rd_size * 1024);
281
                rd_blocksizes[i] = 1024;
282
        }
283
 
284
        blksize_size[MAJOR_NR] = rd_blocksizes;
285
 
286
        printk("Ramdisk driver initialized : %d ramdisks of %dK size\n",
287
                                                        NUM_RAMDISKS, rd_size);
288
 
289
        return 0;
290
}
291
 
292
/* loadable module support */
293
 
294
#ifdef MODULE
295
 
296
int init_module(void)
297
{
298
        int error = rd_init();
299
        if (!error)
300
                printk(KERN_INFO "RAMDISK: Loaded as module.\n");
301
        return error;
302
}
303
 
304
/* Before freeing the module, invalidate all of the protected buffers! */
305
void cleanup_module(void)
306
{
307
        int i;
308
 
309
        for (i = 0 ; i < NUM_RAMDISKS; i++)
310
                invalidate_buffers(MKDEV(MAJOR_NR, i));
311
 
312
        unregister_blkdev( MAJOR_NR, "ramdisk" );
313
        blk_dev[MAJOR_NR].request_fn = 0;
314
}
315
 
316
#endif  /* MODULE */
317
 
318
/* End of non-loading portions of the ramdisk driver */
319
 
320
#ifdef RD_LOADER 
321
/*
322
 * This routine tries to a ramdisk image to load, and returns the
323
 * number of blocks to read for a non-compressed image, 0 if the image
324
 * is a compressed image, and -1 if an image with the right magic
325
 * numbers could not be found.
326
 *
327
 * We currently check for the following magic numbers:
328
 *      minix
329
 *      ext2
330
 *      gzip
331
 */
332
int
333
identify_ramdisk_image(kdev_t device, struct file *fp, int start_block)
334
{
335
        const int size = 512;
336
        struct minix_super_block *minixsb;
337
        struct ext2_super_block *ext2sb;
338
        int nblocks = -1;
339
        int max_blocks;
340
        unsigned char *buf;
341
 
342
        buf = kmalloc(size, GFP_KERNEL);
343
        if (buf == 0)
344
                return -1;
345
 
346
        minixsb = (struct minix_super_block *) buf;
347
        ext2sb = (struct ext2_super_block *) buf;
348
        memset(buf, 0xe5, size);
349
 
350
        /*
351
         * Read block 0 to test for gzipped kernel
352
         */
353
        if (fp->f_op->lseek)
354
                fp->f_op->lseek(fp->f_inode, fp, start_block * BLOCK_SIZE, 0);
355
        fp->f_pos = start_block * BLOCK_SIZE;
356
 
357
        fp->f_op->read(fp->f_inode, fp, buf, size);
358
 
359
        /*
360
         * If it matches the gzip magic numbers, return -1
361
         */
362
        if (buf[0] == 037 && ((buf[1] == 0213) || (buf[1] == 0236))) {
363
                printk(KERN_NOTICE
364
                       "RAMDISK: Compressed image found at block %d\n",
365
                       start_block);
366
                nblocks = 0;
367
                goto done;
368
        }
369
 
370
        /*
371
         * Read block 1 to test for minix and ext2 superblock
372
         */
373
        if (fp->f_op->lseek)
374
                fp->f_op->lseek(fp->f_inode, fp,
375
                                (start_block+1) * BLOCK_SIZE, 0);
376
        fp->f_pos = (start_block+1) * BLOCK_SIZE;
377
 
378
        fp->f_op->read(fp->f_inode, fp, buf, size);
379
 
380
        /* Try minix */
381
        if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
382
            minixsb->s_magic == MINIX_SUPER_MAGIC2) {
383
                printk(KERN_NOTICE
384
                       "RAMDISK: Minix filesystem found at block %d\n",
385
                       start_block);
386
                nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
387
                goto done;
388
        }
389
 
390
        /* Try ext2 */
391
        if (ext2sb->s_magic == EXT2_SUPER_MAGIC) {
392
                printk(KERN_NOTICE
393
                       "RAMDISK: Ext2 filesystem found at block %d\n",
394
                       start_block);
395
                nblocks = ext2sb->s_blocks_count;
396
                goto done;
397
        }
398
        printk(KERN_NOTICE
399
               "RAMDISK: Couldn't find valid ramdisk image starting at %d.\n",
400
               start_block);
401
 
402
done:
403
        if (fp->f_op->lseek)
404
                fp->f_op->lseek(fp->f_inode, fp, start_block * BLOCK_SIZE, 0);
405
        fp->f_pos = start_block * BLOCK_SIZE;
406
 
407
        if ((nblocks > 0) && blk_size[MAJOR(device)]) {
408
                max_blocks = blk_size[MAJOR(device)][MINOR(device)];
409
                max_blocks -= start_block;
410
                if (nblocks > max_blocks) {
411
                        printk(KERN_NOTICE
412
                               "RAMDISK: Restricting filesystem size "
413
                               "from %d to %d blocks.\n",
414
                               nblocks, max_blocks);
415
                        nblocks = max_blocks;
416
                }
417
        }
418
        kfree(buf);
419
        return nblocks;
420
}
421
 
422
/*
423
 * This routine loads in the ramdisk image.
424
 */
425
static void rd_load_image(kdev_t device,int offset, int unit)
426
{
427
        struct inode inode, out_inode;
428
        struct file infile, outfile;
429
        unsigned short fs;
430
        kdev_t ram_device;
431
        int nblocks, i;
432
        char *buf;
433
        unsigned short rotate = 0;
434
        char rotator[4] = { '|' , '/' , '-' , '\\' };
435
 
436
        ram_device = MKDEV(MAJOR_NR, unit);
437
 
438
        memset(&infile, 0, sizeof(infile));
439
        memset(&inode, 0, sizeof(inode));
440
        inode.i_rdev = device;
441
        infile.f_mode = 1; /* read only */
442
        infile.f_inode = &inode;
443
 
444
        memset(&outfile, 0, sizeof(outfile));
445
        memset(&out_inode, 0, sizeof(out_inode));
446
        out_inode.i_rdev = ram_device;
447
        outfile.f_mode = 3; /* read/write */
448
        outfile.f_inode = &out_inode;
449
 
450
        if (blkdev_open(&inode, &infile) != 0) return;
451
        if (blkdev_open(&out_inode, &outfile) != 0) return;
452
 
453
        fs = get_fs();
454
        set_fs(KERNEL_DS);
455
 
456
        nblocks = identify_ramdisk_image(device, &infile, offset);
457
        if (nblocks < 0)
458
                goto done;
459
 
460
        if (nblocks == 0) {
461
#ifdef BUILD_CRAMDISK
462
                if (crd_load(&infile, &outfile) == 0)
463
                        goto successful_load;
464
#else
465
                printk(KERN_NOTICE
466
                       "RAMDISK: Kernel does not support compressed "
467
                       "ramdisk images\n");
468
#endif
469
                goto done;
470
        }
471
 
472
        if (nblocks > (rd_length[0] >> BLOCK_SIZE_BITS)) {
473
                printk("RAMDISK: image too big! (%d/%d blocks)\n",
474
                       nblocks, rd_length[0] >> BLOCK_SIZE_BITS);
475
                goto done;
476
        }
477
 
478
        /*
479
         * OK, time to copy in the data
480
         */
481
        buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
482
        if (buf == 0) {
483
                printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
484
                goto done;
485
        }
486
 
487
        printk(KERN_NOTICE "RAMDISK: Loading %d blocks into ram disk... ", nblocks);
488
        for (i=0; i < nblocks; i++) {
489
                infile.f_op->read(infile.f_inode, &infile, buf,
490
                                  BLOCK_SIZE);
491
                outfile.f_op->write(outfile.f_inode, &outfile, buf,
492
                                    BLOCK_SIZE);
493
                if (!(i % 16)) {
494
                        printk("%c\b", rotator[rotate & 0x3]);
495
                        rotate++;
496
                }
497
        }
498
        printk("done.\n");
499
        kfree(buf);
500
 
501
successful_load:
502
        invalidate_buffers(device);
503
        ROOT_DEV = MKDEV(MAJOR_NR,unit);
504
 
505
done:
506
        if (infile.f_op->release)
507
                infile.f_op->release(&inode, &infile);
508
        set_fs(fs);
509
}
510
 
511
 
512
static void rd_load_disk(int n)
513
{
514
#ifdef CONFIG_BLK_DEV_INITRD
515
        extern kdev_t real_root_dev;
516
#endif  
517
 
518
        if (rd_doload == 0)
519
                return;
520
 
521
        if (MAJOR(ROOT_DEV) != FLOPPY_MAJOR
522
#ifdef CONFIG_BLK_DEV_INITRD    
523
                && MAJOR(real_root_dev) != FLOPPY_MAJOR
524
#endif          
525
        )
526
                        return;
527
 
528
        if (rd_prompt) {
529
#ifdef CONFIG_BLK_DEV_FD
530
                floppy_eject();
531
#endif
532
                printk(KERN_NOTICE
533
                       "VFS: Insert root floppy disk to be loaded into ramdisk and press ENTER\n");
534
                wait_for_keypress();
535
        }
536
 
537
        rd_load_image(ROOT_DEV,rd_image_start,n);
538
 
539
}
540
 
541
void rd_load(void)
542
{
543
        rd_load_disk(0);
544
}
545
 
546
void rd_load_secondary(void)
547
{
548
        rd_load_disk(1);
549
}
550
 
551
#ifdef CONFIG_BLK_DEV_INITRD
552
void initrd_load(void)
553
{
554
        rd_load_image(MKDEV(MAJOR_NR, INITRD_MINOR),0,0);
555
}
556
#endif
557
 
558
#endif /* RD_LOADER */
559
 
560
#ifdef BUILD_CRAMDISK
561
 
562
/*
563
 * gzip declarations
564
 */
565
 
566
#define OF(args)  args
567
 
568
#define memzero(s, n)     memset ((s), 0, (n))
569
 
570
 
571
typedef unsigned char  uch;
572
typedef unsigned short ush;
573
typedef unsigned long  ulg;
574
 
575
#define INBUFSIZ 4096
576
#define WSIZE 0x8000    /* window size--must be a power of two, and */
577
                        /*  at least 32K for zip's deflate method */
578
 
579
static uch *inbuf;
580
static uch *window;
581
 
582
static unsigned insize = 0;  /* valid bytes in inbuf */
583
static unsigned inptr = 0;   /* index of next byte to be processed in inbuf */
584
static unsigned outcnt = 0;  /* bytes in output buffer */
585
static int exit_code = 0;
586
static long bytes_out = 0;
587
static struct file *crd_infp, *crd_outfp;
588
 
589
#define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf())
590
 
591
/* Diagnostic functions (stubbed out) */
592
#define Assert(cond,msg)
593
#define Trace(x)
594
#define Tracev(x)
595
#define Tracevv(x)
596
#define Tracec(c,x)
597
#define Tracecv(c,x)
598
 
599
#define STATIC static
600
 
601
static int  fill_inbuf(void);
602
static void flush_window(void);
603
static void *malloc(int size);
604
static void free(void *where);
605
static void error(char *m);
606
static void gzip_mark(void **);
607
static void gzip_release(void **);
608
 
609
#include "../../../../lib/inflate.c"
610
 
611
static void *malloc(int size)
612
{
613
        return kmalloc(size, GFP_KERNEL);
614
}
615
 
616
static void free(void *where)
617
{
618
        kfree(where);
619
}
620
 
621
static void gzip_mark(void **ptr)
622
{
623
}
624
 
625
static void gzip_release(void **ptr)
626
{
627
}
628
 
629
 
630
/* ===========================================================================
631
 * Fill the input buffer. This is called only when the buffer is empty
632
 * and at least one byte is really needed.
633
 */
634
static int fill_inbuf()
635
{
636
        if (exit_code) return -1;
637
 
638
        insize = crd_infp->f_op->read(crd_infp->f_inode, crd_infp,
639
                                      inbuf, INBUFSIZ);
640
        if (insize == 0) return -1;
641
 
642
        inptr = 1;
643
 
644
        return inbuf[0];
645
}
646
 
647
/* ===========================================================================
648
 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
649
 * (Used for the decompressed data only.)
650
 */
651
static void flush_window()
652
{
653
    ulg c = crc;         /* temporary variable */
654
    unsigned n;
655
    uch *in, ch;
656
 
657
    crd_outfp->f_op->write(crd_outfp->f_inode, crd_outfp, window,
658
                           outcnt);
659
    in = window;
660
    for (n = 0; n < outcnt; n++) {
661
            ch = *in++;
662
            c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
663
    }
664
    crc = c;
665
    bytes_out += (ulg)outcnt;
666
    outcnt = 0;
667
}
668
 
669
static void error(char *x)
670
{
671
        printk(KERN_ERR "%s", x);
672
        exit_code = 1;
673
}
674
 
675
static int
676
crd_load(struct file * fp, struct file *outfp)
677
{
678
        int result;
679
 
680
        insize = 0;  /* valid bytes in inbuf */
681
        inptr = 0;   /* index of next byte to be processed in inbuf */
682
        outcnt = 0;  /* bytes in output buffer */
683
        exit_code = 0;
684
        bytes_out = 0;
685
        crc = 0xFFFFFFFF;
686
 
687
        crd_infp = fp;
688
        crd_outfp = outfp;
689
        inbuf = kmalloc(INBUFSIZ, GFP_KERNEL);
690
        if (inbuf == 0) {
691
                printk(KERN_ERR "RAMDISK: Couldn't allocate gzip buffer\n");
692
                return -1;
693
        }
694
        window = kmalloc(WSIZE, GFP_KERNEL);
695
        if (window == 0) {
696
                printk(KERN_ERR "RAMDISK: Couldn't allocate gzip window\n");
697
                kfree(inbuf);
698
                return -1;
699
        }
700
        makecrc();
701
        result = gunzip();
702
        kfree(inbuf);
703
        kfree(window);
704
        return result;
705
}
706
 
707
#endif  /* BUILD_CRAMDISK */
708
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.