kern
diff 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 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/bdev.c Fri Dec 09 13:44:15 2011 +0200 1.3 @@ -0,0 +1,86 @@ 1.4 +#include <stdlib.h> 1.5 +#include <assert.h> 1.6 +#include "bdev.h" 1.7 +#include "ata.h" 1.8 +#include "part.h" 1.9 + 1.10 +#define MINOR_DISK(x) (((x) >> 4) & 0xf) 1.11 +#define MINOR_PART(x) ((x) & 0xf) 1.12 + 1.13 +struct block_device *blk_open(dev_t dev) 1.14 +{ 1.15 + struct block_device *bdev; 1.16 + int i, minor, devno, part; 1.17 + 1.18 + /* XXX for now ignore the major number as we only have ata devices */ 1.19 + minor = DEV_MINOR(dev); 1.20 + devno = MINOR_DISK(minor); 1.21 + part = MINOR_PART(minor); 1.22 + 1.23 + bdev = malloc(sizeof *bdev); 1.24 + assert(bdev); 1.25 + 1.26 + bdev->ata_dev = devno; 1.27 + 1.28 + if(part) { 1.29 + struct partition *plist = get_part_list(devno); 1.30 + assert(plist); 1.31 + 1.32 + for(i=1; i<part; i++) { 1.33 + if(!plist) break; 1.34 + plist = plist->next; 1.35 + } 1.36 + if(!plist) { 1.37 + free(bdev); 1.38 + free_part_list(plist); 1.39 + return 0; 1.40 + } 1.41 + 1.42 + bdev->offset = SECT_TO_BLK(plist->start_sect); 1.43 + bdev->size = SECT_TO_BLK(plist->size_sect); 1.44 + 1.45 + free_part_list(plist); 1.46 + } else { 1.47 + bdev->offset = 0; 1.48 + bdev->size = SECT_TO_BLK(ata_num_sectors(devno)); 1.49 + } 1.50 + 1.51 + return bdev; 1.52 +} 1.53 + 1.54 +void blk_close(struct block_device *bdev) 1.55 +{ 1.56 + free(bdev); 1.57 +} 1.58 + 1.59 +#define NSECT (BLKSZ / 512) 1.60 + 1.61 +int blk_read(struct block_device *bdev, uint32_t blk, void *buf) 1.62 +{ 1.63 + int i; 1.64 + char *ptr = buf; 1.65 + uint32_t sect = blk * NSECT; 1.66 + 1.67 + for(i=0; i<NSECT; i++) { 1.68 + if(ata_read_pio(bdev->ata_dev, sect++, ptr) == -1) { 1.69 + return -1; 1.70 + } 1.71 + ptr += 512; 1.72 + } 1.73 + return 0; 1.74 +} 1.75 + 1.76 +int blk_write(struct block_device *bdev, uint32_t blk, void *buf) 1.77 +{ 1.78 + int i; 1.79 + char *ptr = buf; 1.80 + uint32_t sect = blk * NSECT; 1.81 + 1.82 + for(i=0; i<NSECT; i++) { 1.83 + if(ata_write_pio(bdev->ata_dev, sect++, ptr) == -1) { 1.84 + return -1; 1.85 + } 1.86 + ptr += 512; 1.87 + } 1.88 + return 0; 1.89 +}