/* * Writes buf into cluster, buf should be holding a cluster's worth of data, * that is it should have been allocated to size cluster_size(cluster). * * @param cluster cluster to write to * @param *buf buffer holding the data to write * * @return 0 if successful, otherwise -1 */ static int ll_fd_write(int cluster, char *buf) { int offset = ctobpos(cluster); int size = cluster_size(cluster); if (lseek(fd, offset, SEEK_SET) != offset) return -1; if (write(fd, buf, size) != size) return -1; return 0; }
int read_content(char *dst, char *src, size_t cnt, unsigned int cluster) { int clustersize; char *vptr; //char *k; //k = calloc(cnt + 1, 1); clustersize = cluster_size(src); vptr = cluster_addr(src, cluster); printf("[read_content] cluster=%d,clustersize=%d, cnt=%d, offset=%x\n", cluster, clustersize, cnt, vptr - src); //memcpy(k, vptr, cnt); memcpy(dst, vptr, cnt); //printf(stderr, "%s",k); //free(k); return cnt; }
char * cluster_addr(char *baseaddr, int cluster) { return cluster_2(baseaddr) + (cluster - 2) * cluster_size(baseaddr); }
char * do_job(int *fsize, char *inbuf) { /*int fs_type;*/ char *fat_base; char *entrys, *entry; int cluster; int filesize ; char *name; char *fbuf; int cnt, ret; char *pret; // debug ret = mbr_sector_size(inbuf); printf(" [mbr_sector_size] = %u\n", ret); pret = mbr_fat(inbuf); printf(" [mbr_fat] base=0x%x, fat=0x%x, offset=%d\n", (unsigned int)inbuf, (unsigned int)pret, pret - inbuf); ret = mbr_fat_cnt(inbuf); printf(" [mbr_fat_cnt] ret=%u\n", (unsigned)ret); ret = mbr_sectors_peer_fat(inbuf); printf(" [mbr_sectors_peer_fat] ret=%u\n", (unsigned)ret); ret = mbr_sectors_peer_cluster(inbuf); printf(" [mbr_sectors_peer_cluster] = %u\n", (unsigned)ret); pret = mbr_entrys(inbuf); printf(" [mbr_entrys] base=0x%x, entrys=0x%x, offset=%d\n", (unsigned int)inbuf, (unsigned int)pret, pret - inbuf); // debug end // get fat,entry 's address fat_base = mbr_fat(inbuf); printf("fat_base=%d\n", (unsigned)(fat_base - inbuf)); entrys = mbr_entrys(inbuf); printf("entrys=%d\n", (unsigned)(entrys - inbuf)); // value init cluster = -1; filesize = 0; // find uboot entry entry = entrys; while ( entry_is_null(entry) != 1) { name = entry_name(entry); if (strncmp(name, "UBOOT", 5) == 0) { printf("-------------get uboot------------\n"); cluster = entry_cluster(entry); filesize = entry_fsize(entry); break; } entry = entry_next_entry(entry); printf(" entry =%d\n", (entry - entry)); } if (cluster == -1) { printf("can't find file: UBOOT\n"); return NULL; } printf("begin read file\n"); // read uboot fbuf = malloc(filesize); cnt = 0; while (is_endofile(cluster) != 1) { cnt += read_content(fbuf + cnt, inbuf, min(filesize - cnt, cluster_size(inbuf)), cluster); cluster = next_cluster(fat_base, cluster); } *fsize = filesize; return fbuf; }