kern
changeset 89:2f555c81ae67
starting the filesystem
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 08 Dec 2011 18:19:35 +0200 |
parents | a398bf73fe93 |
children | 7ff2b4971216 |
files | src/ata.c src/fs.c src/fs.h |
diffstat | 3 files changed, 71 insertions(+), 21 deletions(-) [+] |
line diff
1.1 --- a/src/ata.c Thu Dec 08 13:34:47 2011 +0200 1.2 +++ b/src/ata.c Thu Dec 08 18:19:35 2011 +0200 1.3 @@ -53,11 +53,13 @@ 1.4 }; 1.5 1.6 1.7 +static int readwrite_pio(int devno, uint64_t sect, void *buf, void (*rwdata)(struct device*, void*)); 1.8 static int identify(struct device *dev, int iface, int id); 1.9 static void select_dev(struct device *dev); 1.10 static int wait_busy(struct device *dev); 1.11 static int wait_drq(struct device *dev); 1.12 static void read_data(struct device *dev, void *buf); 1.13 +static void write_data(struct device *dev, void *buf); 1.14 static inline uint8_t read_reg8(struct device *dev, int reg); 1.15 static inline uint16_t read_reg16(struct device *dev, int reg); 1.16 static inline void write_reg8(struct device *dev, int reg, uint8_t val); 1.17 @@ -110,6 +112,16 @@ 1.18 1.19 int ata_read_pio(int devno, uint64_t sect, void *buf) 1.20 { 1.21 + return readwrite_pio(devno, sect, buf, read_data); 1.22 +} 1.23 + 1.24 +int ata_write_pio(int devno, uint64_t sect, void *buf) 1.25 +{ 1.26 + return readwrite_pio(devno, sect, buf, write_data); 1.27 +} 1.28 + 1.29 +static int readwrite_pio(int devno, uint64_t sect, void *buf, void (*rwdata)(struct device*, void*)) 1.30 +{ 1.31 int use_irq, cmd, st, res = -1; 1.32 uint32_t sect_low, sect_high; 1.33 struct device *dev = devices + devno; 1.34 @@ -171,8 +183,8 @@ 1.35 goto end; 1.36 } 1.37 1.38 - /* read the data and we're done */ 1.39 - read_data(dev, buf); 1.40 + /* read/write the data and we're done */ 1.41 + rwdata(dev, buf); 1.42 res = 0; 1.43 end: 1.44 if(use_irq) { 1.45 @@ -181,14 +193,6 @@ 1.46 return res; 1.47 } 1.48 1.49 -int ata_write_pio(int devno, uint64_t sect, void *buf) 1.50 -{ 1.51 - if(devices[devno].id == -1) { 1.52 - return -1; 1.53 - } 1.54 - return -1; 1.55 -} 1.56 - 1.57 static int identify(struct device *dev, int iface, int id) 1.58 { 1.59 /* base address of the two ATA interfaces */ 1.60 @@ -301,6 +305,20 @@ 1.61 } 1.62 } 1.63 1.64 +static void write_data(struct device *dev, void *buf) 1.65 +{ 1.66 + int i; 1.67 + uint16_t *ptr = buf; 1.68 + 1.69 + /* wait for the data request from the device */ 1.70 + wait_drq(dev); 1.71 + 1.72 + /* ready to transfer */ 1.73 + for(i=0; i<256; i++) { 1.74 + write_reg16(dev, REG_DATA, *ptr++); 1.75 + } 1.76 +} 1.77 + 1.78 static inline uint8_t read_reg8(struct device *dev, int reg) 1.79 { 1.80 uint8_t val;
2.1 --- a/src/fs.c Thu Dec 08 13:34:47 2011 +0200 2.2 +++ b/src/fs.c Thu Dec 08 18:19:35 2011 +0200 2.3 @@ -1,25 +1,50 @@ 2.4 #include <stdio.h> 2.5 +#include <stdlib.h> 2.6 +#include <assert.h> 2.7 #include "fs.h" 2.8 #include "ata.h" 2.9 #include "part.h" 2.10 #include "panic.h" 2.11 2.12 -#define PART_TYPE 0xcc 2.13 +#define MAGIC 0xccf5ccf5 2.14 +#define BLKSZ 1024 2.15 2.16 -static int find_rootfs(int *dev, struct partition *part); 2.17 +typedef uint32_t blkid; 2.18 + 2.19 +struct superblock { 2.20 + uint32_t magic; 2.21 + 2.22 + blkid istart; 2.23 + unsigned int icount; 2.24 + 2.25 + blkid dstart; 2.26 + unsigned int dcount; 2.27 +}; 2.28 + 2.29 +struct filesys { 2.30 + int dev; 2.31 + struct partition part; 2.32 + 2.33 + struct superblock *sb; 2.34 +}; 2.35 + 2.36 +static int find_rootfs(struct filesys *fs); 2.37 2.38 /* root device & partition */ 2.39 -static int rdev; 2.40 -static struct partition rpart; 2.41 +static struct filesys root; 2.42 2.43 void init_fs(void) 2.44 { 2.45 - if(find_rootfs(&rdev, &rpart) == -1) { 2.46 + root.sb = malloc(512); 2.47 + assert(root.sb); 2.48 + 2.49 + if(find_rootfs(&root) == -1) { 2.50 panic("can't find root filesystem\n"); 2.51 } 2.52 } 2.53 2.54 -static int find_rootfs(int *dev, struct partition *part) 2.55 +#define PART_TYPE 0xcc 2.56 +static int find_rootfs(struct filesys *fs) 2.57 { 2.58 int i, num_dev, partid; 2.59 struct partition *plist, *p; 2.60 @@ -31,11 +56,17 @@ 2.61 partid = 0; 2.62 while(p) { 2.63 if(get_part_type(p) == PART_TYPE) { 2.64 - /* found it! */ 2.65 - printf("using ata%dp%d\n", i, partid); 2.66 - *dev = i; 2.67 - *part = *p; 2.68 - return 0; 2.69 + /* found the correct partition, now read the superblock 2.70 + * and make sure it's got the correct magic id 2.71 + */ 2.72 + ata_read_pio(i, p->start_sect + 2, fs->sb); 2.73 + 2.74 + if(fs->sb->magic == MAGIC) { 2.75 + printf("found root ata%dp%d\n", i, partid); 2.76 + fs->dev = i; 2.77 + fs->part = *p; 2.78 + return 0; 2.79 + } 2.80 } 2.81 p = p->next; 2.82 partid++;