nuclear@90: #include nuclear@91: #include nuclear@90: #include nuclear@90: #include "bdev.h" nuclear@90: #include "ata.h" nuclear@90: #include "part.h" nuclear@90: nuclear@91: #define MKMINOR(disk, part) ((((disk) & 0xf) << 4) | ((part) & 0xf)) nuclear@91: #define MINOR_DISK(x) (((x) >> 4) & 0xf) nuclear@91: #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@93: bdev->ptype = get_part_type(plist); 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@93: bdev->ptype = 0; 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@93: int blk_read(struct block_device *bdev, uint32_t blk, int count, void *buf) nuclear@90: { nuclear@90: int i; nuclear@90: char *ptr = buf; nuclear@93: uint32_t sect = blk * NSECT + bdev->offset; nuclear@90: nuclear@93: 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@93: int blk_write(struct block_device *bdev, uint32_t blk, int count, void *buf) nuclear@90: { nuclear@90: int i; nuclear@90: char *ptr = buf; nuclear@93: uint32_t sect = blk * NSECT + bdev->offset; nuclear@90: nuclear@93: 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@91: nuclear@91: dev_t bdev_by_name(const char *name) nuclear@91: { nuclear@91: int minor; nuclear@91: int atadev, part = 0; nuclear@91: nuclear@91: char *tmp = strrchr(name, '/'); nuclear@91: if(tmp) { nuclear@91: name = tmp + 1; nuclear@91: } nuclear@91: nuclear@91: if(strstr(name, "ata") != name) { nuclear@91: return 0; nuclear@91: } nuclear@91: name += 3; nuclear@91: nuclear@91: atadev = strtol(name, &tmp, 10); nuclear@91: if(tmp == name) { nuclear@91: return 0; nuclear@91: } nuclear@91: name = tmp; nuclear@91: nuclear@91: if(*name++ == 'p') { nuclear@91: part = strtol(name, &tmp, 10) + 1; nuclear@91: if(tmp == name) { nuclear@91: return 0; nuclear@91: } nuclear@91: } nuclear@91: nuclear@91: minor = MKMINOR(atadev, part); nuclear@93: return DEVNO(1, minor); nuclear@91: }