nuclear@90: #include nuclear@90: #include nuclear@90: #include "bdev.h" nuclear@90: #include "ata.h" nuclear@90: #include "part.h" nuclear@90: nuclear@90: #define MINOR_DISK(x) (((x) >> 4) & 0xf) nuclear@90: #define MINOR_PART(x) ((x) & 0xf) nuclear@90: nuclear@90: struct block_device *blk_open(dev_t dev) nuclear@90: { nuclear@90: struct block_device *bdev; nuclear@90: int i, minor, devno, part; nuclear@90: nuclear@90: /* XXX for now ignore the major number as we only have ata devices */ nuclear@90: minor = DEV_MINOR(dev); nuclear@90: devno = MINOR_DISK(minor); nuclear@90: part = MINOR_PART(minor); nuclear@90: nuclear@90: bdev = malloc(sizeof *bdev); nuclear@90: assert(bdev); nuclear@90: nuclear@90: bdev->ata_dev = devno; nuclear@90: nuclear@90: if(part) { nuclear@90: struct partition *plist = get_part_list(devno); nuclear@90: assert(plist); nuclear@90: nuclear@90: for(i=1; inext; nuclear@90: } nuclear@90: if(!plist) { nuclear@90: free(bdev); nuclear@90: free_part_list(plist); nuclear@90: return 0; nuclear@90: } nuclear@90: nuclear@90: bdev->offset = SECT_TO_BLK(plist->start_sect); nuclear@90: bdev->size = SECT_TO_BLK(plist->size_sect); nuclear@90: nuclear@90: free_part_list(plist); nuclear@90: } else { nuclear@90: bdev->offset = 0; nuclear@90: bdev->size = SECT_TO_BLK(ata_num_sectors(devno)); nuclear@90: } nuclear@90: nuclear@90: return bdev; nuclear@90: } nuclear@90: nuclear@90: void blk_close(struct block_device *bdev) nuclear@90: { nuclear@90: free(bdev); nuclear@90: } nuclear@90: nuclear@90: #define NSECT (BLKSZ / 512) nuclear@90: nuclear@90: int blk_read(struct block_device *bdev, uint32_t blk, void *buf) nuclear@90: { nuclear@90: int i; nuclear@90: char *ptr = buf; nuclear@90: uint32_t sect = blk * NSECT; nuclear@90: nuclear@90: for(i=0; iata_dev, sect++, ptr) == -1) { nuclear@90: return -1; nuclear@90: } nuclear@90: ptr += 512; nuclear@90: } nuclear@90: return 0; nuclear@90: } nuclear@90: nuclear@90: int blk_write(struct block_device *bdev, uint32_t blk, void *buf) nuclear@90: { nuclear@90: int i; nuclear@90: char *ptr = buf; nuclear@90: uint32_t sect = blk * NSECT; nuclear@90: nuclear@90: for(i=0; iata_dev, sect++, ptr) == -1) { nuclear@90: return -1; nuclear@90: } nuclear@90: ptr += 512; nuclear@90: } nuclear@90: return 0; nuclear@90: }