kern
view 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 source
1 #include <stdlib.h>
2 #include <assert.h>
3 #include "bdev.h"
4 #include "ata.h"
5 #include "part.h"
7 #define MINOR_DISK(x) (((x) >> 4) & 0xf)
8 #define MINOR_PART(x) ((x) & 0xf)
10 struct block_device *blk_open(dev_t dev)
11 {
12 struct block_device *bdev;
13 int i, minor, devno, part;
15 /* XXX for now ignore the major number as we only have ata devices */
16 minor = DEV_MINOR(dev);
17 devno = MINOR_DISK(minor);
18 part = MINOR_PART(minor);
20 bdev = malloc(sizeof *bdev);
21 assert(bdev);
23 bdev->ata_dev = devno;
25 if(part) {
26 struct partition *plist = get_part_list(devno);
27 assert(plist);
29 for(i=1; i<part; i++) {
30 if(!plist) break;
31 plist = plist->next;
32 }
33 if(!plist) {
34 free(bdev);
35 free_part_list(plist);
36 return 0;
37 }
39 bdev->offset = SECT_TO_BLK(plist->start_sect);
40 bdev->size = SECT_TO_BLK(plist->size_sect);
42 free_part_list(plist);
43 } else {
44 bdev->offset = 0;
45 bdev->size = SECT_TO_BLK(ata_num_sectors(devno));
46 }
48 return bdev;
49 }
51 void blk_close(struct block_device *bdev)
52 {
53 free(bdev);
54 }
56 #define NSECT (BLKSZ / 512)
58 int blk_read(struct block_device *bdev, uint32_t blk, void *buf)
59 {
60 int i;
61 char *ptr = buf;
62 uint32_t sect = blk * NSECT;
64 for(i=0; i<NSECT; i++) {
65 if(ata_read_pio(bdev->ata_dev, sect++, ptr) == -1) {
66 return -1;
67 }
68 ptr += 512;
69 }
70 return 0;
71 }
73 int blk_write(struct block_device *bdev, uint32_t blk, void *buf)
74 {
75 int i;
76 char *ptr = buf;
77 uint32_t sect = blk * NSECT;
79 for(i=0; i<NSECT; i++) {
80 if(ata_write_pio(bdev->ata_dev, sect++, ptr) == -1) {
81 return -1;
82 }
83 ptr += 512;
84 }
85 return 0;
86 }