kern

view src/bdev.c @ 91:f83f50c17c3b

continuing with the fs added strtol and strstr to klibc
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 09 Dec 2011 15:29:54 +0200
parents 7ff2b4971216
children 083849df660b
line source
1 #include <stdlib.h>
2 #include <string.h>
3 #include <assert.h>
4 #include "bdev.h"
5 #include "ata.h"
6 #include "part.h"
8 #define MKMINOR(disk, part) ((((disk) & 0xf) << 4) | ((part) & 0xf))
9 #define MINOR_DISK(x) (((x) >> 4) & 0xf)
10 #define MINOR_PART(x) ((x) & 0xf)
12 struct block_device *blk_open(dev_t dev)
13 {
14 struct block_device *bdev;
15 int i, minor, devno, part;
17 /* XXX for now ignore the major number as we only have ata devices */
18 minor = DEV_MINOR(dev);
19 devno = MINOR_DISK(minor);
20 part = MINOR_PART(minor);
22 bdev = malloc(sizeof *bdev);
23 assert(bdev);
25 bdev->ata_dev = devno;
27 if(part) {
28 struct partition *plist = get_part_list(devno);
29 assert(plist);
31 for(i=1; i<part; i++) {
32 if(!plist) break;
33 plist = plist->next;
34 }
35 if(!plist) {
36 free(bdev);
37 free_part_list(plist);
38 return 0;
39 }
41 bdev->offset = SECT_TO_BLK(plist->start_sect);
42 bdev->size = SECT_TO_BLK(plist->size_sect);
44 free_part_list(plist);
45 } else {
46 bdev->offset = 0;
47 bdev->size = SECT_TO_BLK(ata_num_sectors(devno));
48 }
50 return bdev;
51 }
53 void blk_close(struct block_device *bdev)
54 {
55 free(bdev);
56 }
58 #define NSECT (BLKSZ / 512)
60 int blk_read(struct block_device *bdev, uint32_t blk, void *buf)
61 {
62 int i;
63 char *ptr = buf;
64 uint32_t sect = blk * NSECT;
66 for(i=0; i<NSECT; i++) {
67 if(ata_read_pio(bdev->ata_dev, sect++, ptr) == -1) {
68 return -1;
69 }
70 ptr += 512;
71 }
72 return 0;
73 }
75 int blk_write(struct block_device *bdev, uint32_t blk, void *buf)
76 {
77 int i;
78 char *ptr = buf;
79 uint32_t sect = blk * NSECT;
81 for(i=0; i<NSECT; i++) {
82 if(ata_write_pio(bdev->ata_dev, sect++, ptr) == -1) {
83 return -1;
84 }
85 ptr += 512;
86 }
87 return 0;
88 }
90 dev_t bdev_by_name(const char *name)
91 {
92 int minor;
93 int atadev, part = 0;
95 char *tmp = strrchr(name, '/');
96 if(tmp) {
97 name = tmp + 1;
98 }
100 if(strstr(name, "ata") != name) {
101 return 0;
102 }
103 name += 3;
105 atadev = strtol(name, &tmp, 10);
106 if(tmp == name) {
107 return 0;
108 }
109 name = tmp;
111 if(*name++ == 'p') {
112 part = strtol(name, &tmp, 10) + 1;
113 if(tmp == name) {
114 return 0;
115 }
116 }
118 minor = MKMINOR(atadev, part);
119 return DEVNO(0, minor);
120 }