kern

annotate src/bdev.c @ 90:7ff2b4971216

started work on the filesystem
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 09 Dec 2011 13:44:15 +0200
parents
children f83f50c17c3b
rev   line source
nuclear@90 1 #include <stdlib.h>
nuclear@90 2 #include <assert.h>
nuclear@90 3 #include "bdev.h"
nuclear@90 4 #include "ata.h"
nuclear@90 5 #include "part.h"
nuclear@90 6
nuclear@90 7 #define MINOR_DISK(x) (((x) >> 4) & 0xf)
nuclear@90 8 #define MINOR_PART(x) ((x) & 0xf)
nuclear@90 9
nuclear@90 10 struct block_device *blk_open(dev_t dev)
nuclear@90 11 {
nuclear@90 12 struct block_device *bdev;
nuclear@90 13 int i, minor, devno, part;
nuclear@90 14
nuclear@90 15 /* XXX for now ignore the major number as we only have ata devices */
nuclear@90 16 minor = DEV_MINOR(dev);
nuclear@90 17 devno = MINOR_DISK(minor);
nuclear@90 18 part = MINOR_PART(minor);
nuclear@90 19
nuclear@90 20 bdev = malloc(sizeof *bdev);
nuclear@90 21 assert(bdev);
nuclear@90 22
nuclear@90 23 bdev->ata_dev = devno;
nuclear@90 24
nuclear@90 25 if(part) {
nuclear@90 26 struct partition *plist = get_part_list(devno);
nuclear@90 27 assert(plist);
nuclear@90 28
nuclear@90 29 for(i=1; i<part; i++) {
nuclear@90 30 if(!plist) break;
nuclear@90 31 plist = plist->next;
nuclear@90 32 }
nuclear@90 33 if(!plist) {
nuclear@90 34 free(bdev);
nuclear@90 35 free_part_list(plist);
nuclear@90 36 return 0;
nuclear@90 37 }
nuclear@90 38
nuclear@90 39 bdev->offset = SECT_TO_BLK(plist->start_sect);
nuclear@90 40 bdev->size = SECT_TO_BLK(plist->size_sect);
nuclear@90 41
nuclear@90 42 free_part_list(plist);
nuclear@90 43 } else {
nuclear@90 44 bdev->offset = 0;
nuclear@90 45 bdev->size = SECT_TO_BLK(ata_num_sectors(devno));
nuclear@90 46 }
nuclear@90 47
nuclear@90 48 return bdev;
nuclear@90 49 }
nuclear@90 50
nuclear@90 51 void blk_close(struct block_device *bdev)
nuclear@90 52 {
nuclear@90 53 free(bdev);
nuclear@90 54 }
nuclear@90 55
nuclear@90 56 #define NSECT (BLKSZ / 512)
nuclear@90 57
nuclear@90 58 int blk_read(struct block_device *bdev, uint32_t blk, void *buf)
nuclear@90 59 {
nuclear@90 60 int i;
nuclear@90 61 char *ptr = buf;
nuclear@90 62 uint32_t sect = blk * NSECT;
nuclear@90 63
nuclear@90 64 for(i=0; i<NSECT; i++) {
nuclear@90 65 if(ata_read_pio(bdev->ata_dev, sect++, ptr) == -1) {
nuclear@90 66 return -1;
nuclear@90 67 }
nuclear@90 68 ptr += 512;
nuclear@90 69 }
nuclear@90 70 return 0;
nuclear@90 71 }
nuclear@90 72
nuclear@90 73 int blk_write(struct block_device *bdev, uint32_t blk, void *buf)
nuclear@90 74 {
nuclear@90 75 int i;
nuclear@90 76 char *ptr = buf;
nuclear@90 77 uint32_t sect = blk * NSECT;
nuclear@90 78
nuclear@90 79 for(i=0; i<NSECT; i++) {
nuclear@90 80 if(ata_write_pio(bdev->ata_dev, sect++, ptr) == -1) {
nuclear@90 81 return -1;
nuclear@90 82 }
nuclear@90 83 ptr += 512;
nuclear@90 84 }
nuclear@90 85 return 0;
nuclear@90 86 }