int fs_write( char *name, const char *data, int length, int offset ) { if(mb.magic != FS_MAGIC){ printf("disk not mounted\n"); return -1; } if(!valid_name(name)){ printf("invalid name\n"); return -1; } dir_entry* file = found_file(name); if(!file){ printf("file not found\n"); return -1; } unsigned first_block = get_block(file, offset); if(first_block == -1){ printf("invalid offset"); return -1; } int bytes_written = 0; unsigned block_offset = offset % DISK_BLOCK_SIZE; unsigned block = first_block; char current_block[DISK_BLOCK_SIZE]; disk_read(first_block, current_block); int to_write = DISK_BLOCK_SIZE; while(bytes_written < length){ if(block == EOFF) { printf("invalid length\n"); return -1; } if(length - bytes_written < DISK_BLOCK_SIZE){ to_write = length - bytes_written; disk_read(block, current_block); } memcpy(current_block + block_offset, data, to_write); disk_write(block, current_block); block_offset = 0; bytes_written += to_write; block = fat[block]; } return bytes_written; }
static quik_err_t read_mac_partition(ihandle dev, unsigned part, part_t *p) { unsigned i; unsigned upart; unsigned blocks_in_map; length_t len; length_t secsize; char blk[SECTOR_SIZE]; struct mac_partition *mp = (struct mac_partition *) blk; struct mac_driver_desc *md = (struct mac_driver_desc *) blk; len = disk_read(dev, blk, SECTOR_SIZE, 0LL); if (len != SECTOR_SIZE) { return ERR_DEV_SHORT_READ; } if (md->signature != MAC_DRIVER_MAGIC) { return ERR_PART_NOT_MAC; } secsize = md->block_size; blocks_in_map = 1; upart = 0; for (i = 1; i <= blocks_in_map; ++i) { if (disk_read(dev, blk, SECTOR_SIZE, (offset_t) i * secsize) != SECTOR_SIZE) { return ERR_DEV_SHORT_READ; } if (mp->signature != MAC_PARTITION_MAGIC) { break; } if (i == 1) { blocks_in_map = mp->map_count; } ++upart; /* If part is 0, use the first bootable partition. */ if (part == upart || (part == 0 && (mp->status & STATUS_BOOTABLE) != 0 && strcasecmp(mp->processor, "powerpc") == 0)) { p->start = (offset_t) mp->start_block * (offset_t) secsize; p->len = (offset_t) mp->block_count * (offset_t) secsize; p->dev = dev; return ERR_NONE; } } return ERR_PART_NOT_FOUND; }
/* Reads SIZE bytes from INODE into BUFFER, starting at position OFFSET. Returns the number of bytes actually read, which may be less than SIZE if an error occurs or end of file is reached. */ off_t inode_read_at (struct inode *inode, void *buffer_, off_t size, off_t offset) { uint8_t *buffer = buffer_; off_t bytes_read = 0; uint8_t *bounce = NULL; int i=0; while (size > 0) { i++; /* Disk sector to read, starting byte offset within sector. */ disk_sector_t sector_idx = byte_to_sector (inode, offset); int sector_ofs = offset % DISK_SECTOR_SIZE; /* Bytes left in inode, bytes left in sector, lesser of the two. */ off_t inode_left = inode_length (inode) - offset; int sector_left = DISK_SECTOR_SIZE - sector_ofs; int min_left = inode_left < sector_left ? inode_left : sector_left; /* Number of bytes to actually copy out of this sector. */ int chunk_size = size < min_left ? size : min_left; if (chunk_size <= 0) break; if (sector_ofs == 0 && chunk_size == DISK_SECTOR_SIZE) { /* Read full sector directly into caller's buffer. */ disk_read (filesys_disk, sector_idx, buffer + bytes_read); } else { /* Read sector into bounce buffer, then partially copy into caller's buffer. */ if (bounce == NULL) { bounce = malloc (DISK_SECTOR_SIZE); if (bounce == NULL) break; } disk_read (filesys_disk, sector_idx, bounce); memcpy (buffer + bytes_read, bounce + sector_ofs, chunk_size); } /* Advance. */ size -= chunk_size; offset += chunk_size; bytes_read += chunk_size; } free (bounce); return bytes_read; }
static int load_super () { int err; while (1) { err = disk_read (0, 2, 2, sb_block, seg_data ()); //if (err) break; sb_data = (struct super_block *) sb_block; /* if (sb_data->s_log_zone_size) { //log_err ("zone size"); err = -1; break; } */ ib_first = 2 + sb_data->s_imap_blocks + sb_data->s_zmap_blocks; /* if (sb_data->s_magic == SUPER_MAGIC) { dir_32 = 0; } else if (sb_data->s_magic == SUPER_MAGIC2) { dir_32 = 1; } else { //log_err ("super magic"); err = -1; } */ break; } return err; }
BTNode* BTree_search(const BTree tree, int key, int* pos) { if (!tree) { return NULL; } int i = 0; while (i < tree->keynum && key > tree->key[i]) { ++i; } // Find the key. if (i < tree->keynum && tree->key[i] == key) { if (pos) { *pos = i; } return tree; } // tree 为叶子节点,找不到 key,查找失败返回 if (tree->isLeaf) { return NULL; } // 节点内查找失败,但 tree->key[i - 1]< key < tree->key[i], // 下一个查找的结点应为 child[i] // 从磁盘读取第 i 个孩子的数据 disk_read(&tree->child[i]); // 递归地继续查找于树 tree->child[i] return BTree_search(tree->child[i], key, pos); }
static int ioread(void *addr, int count, int phys) { int logno, off, size; while (count) { off = blkoff(fs, poff); logno = lblkno(fs, poff); cnt = size = blksize(fs, &inode, logno); bnum = block_map(logno); if (bnum == -1) return(1); bnum = fsbtodb(fs, bnum) + boff; size -= off; if (size > count) size = count; if (disk_read(bnum, cnt, (vm_offset_t)iobuf)) return(1); if (phys) pcpy(iobuf+off,addr,size); else bcopy(iobuf+off,addr,size); addr = (char *)addr + size; count -= size; poff += size; } return(0); }
/******************************************************************************* * Function Name : MAL_Read * Description : Read sectors * Input : None * Output : None * Return : Buffer pointer *******************************************************************************/ uint16_t MAL_Read(uint8_t lun, uint32_t Memory_Offset, volatile uint8_t * Readbuff, uint32_t Transfer_Length) { switch (lun) { case 0: /* Physical drive number (0) */ Status = disk_read (0, (volatile uint8_t*)Readbuff, Memory_Offset/512, Transfer_Length/512); //Status = SD_ReadBlock((uint8_t*)Readbuff, Memory_Offset, Transfer_Length); #ifdef USE_STM3210E_EVAL if ( Status != SD_OK ) { return MAL_FAIL; } #endif /* USE_STM3210E_EVAL */ if(Status) return MAL_FAIL; break; #ifdef USE_STM3210E_EVAL case 1: NAND_Read(Memory_Offset, Readbuff, Transfer_Length); ; break; #endif default: return MAL_FAIL; } return MAL_OK; }
// all the used swap slots are set to TRUE void swap_read_and_free (disk_sector_t idx, void *upage) { lock_acquire (&swap_table_lock); // all the swap sectors must be in use // bitmap_all returns true if all pages are in use (i.e. TRUE) // take care of the edge cases ASSERT (idx != SECTOR_ERROR); ASSERT (bitmap_all (swap_table, idx, SLOT_SIZE)); struct disk *swap = disk_get (1,1); if (!swap) PANIC ("No swap disk found\n"); // read in the upage buffer int offset; for (offset = 0; offset < SLOT_SIZE; offset++) { // printf ("Swap read sector %d at address %p\n",idx+offset, upage +(offset * DISK_SECTOR_SIZE)); disk_read (swap, idx + offset, upage + (offset * DISK_SECTOR_SIZE)); } // vacant the swap slot bitmap_set_multiple (swap_table, idx, SLOT_SIZE, false); lock_release (&swap_table_lock); // printf ("Swap read called at %d for %p\n",idx,upage); }
int do_fat_dump (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[]) { __u8 block[1024]; int ret; int bknum; ret = 0; if (argc != 2) { printf ("needs an argument!\n"); return (0); } bknum = simple_strtoul (argv[1], NULL, 10); if (disk_read (0, bknum, block) != 0) { printf ("Error: reading block\n"); return -1; } printf ("FAT dump: %d\n", bknum); hexdump (512, block); return (ret); }
/* Reads an inode from SECTOR and returns a `struct inode' that contains it. Returns a null pointer if memory allocation fails. */ struct inode * inode_open (disk_sector_t sector) { struct list_elem *e; struct inode *inode; /* Check whether this inode is already open. */ for (e = list_begin (&open_inodes); e != list_end (&open_inodes); e = list_next (e)) { inode = list_entry (e, struct inode, elem); if (inode->sector == sector) { inode_reopen (inode); return inode; } } /* Allocate memory. */ inode = malloc (sizeof *inode); if (inode == NULL) return NULL; /* Initialize. */ list_push_front (&open_inodes, &inode->elem); inode->sector = sector; inode->open_cnt = 1; inode->deny_write_cnt = 0; inode->removed = false; disk_read (filesys_disk, inode->sector, &inode->data); return inode; }
int fs_format() { if(mb.magic == FS_MAGIC) { printf("Refusing to format a mounted disk\n"); return -1; } disk_read(0, (char *)&mb); if(mb.magic == FS_MAGIC){ printf("Refusing to format a formatted disk!\n"); mb.magic = 0; return -1; } //Format disk init_super_block(); init_empty_dir(); //Flush disk_write(0, (char*)&mb); disk_write(1, (char*)dir); mb.magic = 0; // mark disk as unmounted return 0; }
void page_fault_helper(struct page_table *pt, int page, int vPage, int vFrame, int flag) { int vFlag; /* get the victim flag */ page_table_get_entry(pt, vPage, &vFrame, &vFlag); /* check for RW flag */ int rw = (PROT_READ|PROT_WRITE); if(vFlag == rw) { /* write victim from physmem to disk */ disk_write(disk, vPage, &physmem[vFrame*PAGE_SIZE]); write_count++; } /* read from disk to victim frame */ disk_read(disk, page, &physmem[vFrame*PAGE_SIZE]); read_count++; /* update page table entries */ page_table_set_entry(pt, page, vFrame, flag); page_table_set_entry(pt, vPage, 0, 0); /* update loaded_pages */ loaded_pages[vFrame] = page; /* Second-Chance clock setting */ if(pageswap == 2 && flag == PROT_READ) { clock[vFrame] = 0; } }
/** @brief Read sectors from a disk. @param bVolNum The volume number of the volume whose block device is being read from. @param ullSectorStart The starting sector number. @param ulSectorCount The number of sectors to read. @param pBuffer The buffer into which to read the sector data. @return A negated ::REDSTATUS code indicating the operation result. @retval 0 Operation was successful. @retval -RED_EIO A disk I/O error occurred. */ static REDSTATUS DiskRead( uint8_t bVolNum, uint64_t ullSectorStart, uint32_t ulSectorCount, void *pBuffer) { REDSTATUS ret = 0; uint32_t ulSectorIdx = 0U; uint32_t ulSectorSize = gaRedVolConf[bVolNum].ulSectorSize; uint8_t *pbBuffer = CAST_VOID_PTR_TO_UINT8_PTR(pBuffer); while(ulSectorIdx < ulSectorCount) { uint32_t ulTransfer = REDMIN(ulSectorCount - ulSectorIdx, MAX_SECTOR_TRANSFER); DRESULT result; result = disk_read(bVolNum, &pbBuffer[ulSectorIdx * ulSectorSize], (DWORD)(ullSectorStart + ulSectorIdx), (BYTE)ulTransfer); if(result != RES_OK) { ret = -RED_EIO; break; } ulSectorIdx += ulTransfer; } return ret; }
static quik_err_t read_dos_partition(ihandle dev, unsigned part, part_t *p) { length_t len; unsigned i; char blk[SECTOR_SIZE]; dos_part_t *d; len = disk_read(dev, blk, SECTOR_SIZE, 0LL); if (len != SECTOR_SIZE) { return ERR_DEV_SHORT_READ; } /* check the MSDOS partition magic */ if ((blk[0x1fe] != 0x55) || (blk[0x1ff] != 0xaa)) { return ERR_PART_NOT_DOS; } if (part >= 4) { return ERR_PART_NOT_FOUND; } d = (dos_part_t *) &blk[0x1be]; for (i = 1; i != part ; i++, d++) { /* nothing */ } p->dev = dev; p->start = swab32(d->start_sect) * SECTOR_SIZE; p->len = swab32(d->nr_sects) * SECTOR_SIZE; return ERR_NONE; }
void USBMSD::memoryVerify (uint8_t * buf, uint16_t size) { uint32_t n; if ((addr + size) > MemorySize) { size = MemorySize - addr; stage = ERROR; stallEndpoint(EPBULK_OUT); } // beginning of a new block -> load a whole block in RAM if (!(addr%BlockSize)) disk_read(page, addr/BlockSize); // info are in RAM -> no need to re-read memory for (n = 0; n < size; n++) { if (page[addr%BlockSize + n] != buf[n]) { memOK = false; break; } } addr += size; length -= size; csw.DataResidue -= size; if ( !length || (stage != PROCESS_CBW)) { csw.Status = (memOK && (stage == PROCESS_CBW)) ? CSW_PASSED : CSW_FAILED; sendCSW(); } }
VOID read_LUN1(VOID) { // A READ operation is underway, and the app has been requested to access the medium. So, call file system to read // to do so. Note this is a low level FatFs call -- we are not attempting to open a file ourselves. The host is // in control of this access, we're just carrying it out. DRESULT dresult = disk_read(0, // Physical drive number (0) RWbuf_info->bufferAddr, // Pointer to the user buffer RWbuf_info->lba, // First LBA of this buffer operation RWbuf_info->lbCount); // The number of blocks being requested as part of this operation // The result of the file system call needs to be communicated to the host. Different file system software uses // different return codes, but they all communicate the same types of results. This code ultimately gets passed to the // host app that issued the command to read (or if the user did it the host OS, perhaps in a dialog box). switch(dresult) { case RES_OK: RWbuf_info->returnCode = kUSBMSC_RWSuccess; break; case RES_ERROR: // In FatFs, this result suggests the medium may have been removed recently. if(!checkInsertionRemoval()) // This application function checks for the SD-card, and if missing, calls USBMSC_updateMediaInfo() to inform the API RWbuf_info->returnCode = kUSBMSC_RWMedNotPresent; break; case RES_NOTRDY: RWbuf_info->returnCode = kUSBMSC_RWNotReady; break; case RES_PARERR: RWbuf_info->returnCode = kUSBMSC_RWLbaOutOfRange; break; } USBMSC_bufferProcessed(); }
_size_t mbr_part(_size_t id) { _mbr_t mbr; _size_t i, n = 0; if ((disk_read(id, &mbr, sizeof(_mbr_t), 0x1be) != -1) && (mbr.sign == 0xaa55)) { debug("Disk %d: Boot signature", id); for (i = 0; i < 4; i++) { if (mbr.part[i].part_type == TYPE_EXT) { debug("Disk %d: DOS Partition %d, Extended", id, i); n += mbr_part(disk_mmap(id, mbr.part[i].first_sector_lba, mbr.part[i].sector_num)); n++; } else if (mbr.part[i].part_type == TYPE_GPT) { n += gpt_part(id); } else if (mbr.part[i].part_type != TYPE_EMPTY) { debug("Disk %d: DOS Partition %d, Id %02X", id, i, mbr.part[i].part_type); disk_mmap(id, mbr.part[i].first_sector_lba, mbr.part[i].sector_num); n++; } } } return n; }
void USBCDCMSC::memoryRead (void) { uint32_t n; n = (length > MAX_PACKET) ? MAX_PACKET : length; if ((addr + n) > MemorySize) { n = MemorySize - addr; stage = ERROR; } // we read an entire block if (!(addr%BlockSize)) disk_read((char *)page, addr/BlockSize); // write data which are in RAM writeNB(MSDBULK_IN, &page[addr%BlockSize], n, MAX_PACKET_SIZE_EPBULK); addr += n; length -= n; csw.DataResidue -= n; if ( !length || (stage != PROCESS_CBW)) { csw.Status = (stage == PROCESS_CBW) ? CSW_PASSED : CSW_FAILED; stage = (stage == PROCESS_CBW) ? SEND_CSW : stage; } }
static BOOL move_window ( /* TRUE: successful, FALSE: failed */ DWORD sector /* Sector number to make apperance in the FatFs->win */ ) /* Move to zero only writes back dirty window */ { DWORD wsect; FATFS *fs = FatFs; wsect = fs->winsect; if (wsect != sector) { /* Changed current window */ #if !_FS_READONLY BYTE n; if (fs->winflag) { /* Write back dirty window if needed */ if (disk_write(0, fs->win, wsect, 1) != RES_OK) return FALSE; fs->winflag = 0; if (wsect < (fs->fatbase + fs->sects_fat)) { /* In FAT area */ for (n = fs->n_fats; n >= 2; n--) { /* Refrect the change to all FAT copies */ wsect += fs->sects_fat; disk_write(0, fs->win, wsect, 1); } } } #endif if (sector) { if (disk_read(0, fs->win, sector, 1) != RES_OK) return FALSE; fs->winsect = sector; } } return TRUE; }
quik_err_t part_read(part_t *part, offset_t sector, offset_t byte_offset, length_t byte_len, char *buf) { length_t len; offset_t off; off = sector << SECTOR_BITS; /* * Check partition boundaries. */ if ((off + byte_offset + byte_len) >= part->len) { return ERR_PART_BEYOND; } off += part->start + byte_offset; len = disk_read(part->dev, buf, byte_len, off); if (len != byte_len) { return ERR_DEV_SHORT_READ; } return ERR_NONE; }
static void read_fat(int nfatblocks) { int i; if(fat != NULL) return; // read_fat has been called already fat = malloc(N_ADDRESSES_PER_BLOCK * nfatblocks); for(i = 0; i < nfatblocks; ++i) disk_read(2 + i, (char*)(fat + i * N_ADDRESSES_PER_BLOCK)); }
int find(const char *path) { const char *rest; int ch; int block, off, loc, ino = ROOTINO; struct dirent *dp; for (;;) { cnt = fs->fs_bsize; bnum = fsbtodb(fs,itod(fs,ino)) + boff; if (disk_read(bnum, cnt, (vm_offset_t)iobuf)) return 0; bcopy((char *)&((struct dinode *)iobuf)[ino % fs->fs_inopb], (char *)&inode.i_di, sizeof (struct dinode)); if (!*path) return 1; while (*path == '/') path++; if (!inode.i_size || ((inode.i_mode&IFMT) != IFDIR)) return 0; for (rest = path; (ch = *rest) && ch != '/'; rest++) ; loc = 0; for (;;) { if (loc >= inode.i_size) return 0; if (!(off = blkoff(fs, loc))) { block = lblkno(fs, loc); cnt = blksize(fs, &inode, block); bnum = fsbtodb(fs, block_map(block)) + boff; if (disk_read(bnum, cnt, (vm_offset_t)iobuf)) return 0; } dp = (struct dirent *)(iobuf + off); loc += dp->d_reclen; if (dp->d_ino == 0) continue; if (strncmp(path, dp->d_name, rest - path) == 0 && dp->d_name[rest - path] == 0) break; } ino = dp->d_ino; path = rest; } }
void swap_in(uint32_t index, void *frame) { bitmap_flip(swap_bitmap, index); int i; for(i=0;i<SECTOR_PER_PAGE;i++) disk_read(swap_disk, (index * SECTOR_PER_PAGE) + i, frame + (i * DISK_SECTOR_SIZE)); }
void read_sector(int id, int seg, int offset) { int p = (id-1)/18; int q = (id-1)%18; int head = p&1; int track = p>>1; int sector = q+1; disk_read((track<<8)|sector, (head<<8), seg, offset); }
/* * Def: Lit et renvoi le superbloc * In: * device: Numéro du disque * Out: Structure contenant le superbloc */ struct ext2_super_block* ext2_read_sb(struct disk* hd, int s_part) { struct ext2_super_block* sb; sb = (struct ext2_super_block*) malloc(sizeof(struct ext2_super_block)); disk_read(hd->device, s_part + 1024, (char*) sb, sizeof(struct ext2_super_block)); return sb; }
_size_t gpt_part(_size_t id) { _gpt_header_t header; _gpt_entry_t entry; _size_t i, n = 0; if ((disk_read(id, &header, sizeof(_gpt_header_t), get_disk_sector_size(id)) != -1) && (header.sign == 0x5452415020494645)) { debug("Disk %d: GPT Partition Table", id); for (i = 0; i < header.part_entries_num; i++) { if((disk_read(id, &entry, sizeof(_gpt_entry_t), get_disk_sector_size(id) * header.start_part_entries_lba + i * header.part_entry_size) != -1) && (entry.first_lba != 0)) { debug("Disk %d: GPT Partition %d", i); disk_mmap(id, entry.first_lba, entry.last_lba - entry.first_lba + 1); n++; } } } return n; }
int fs_format(){ disk_read(0, (char *)&mb); if(mb.magic == FS_MAGIC){ printf("Refusing to format a formatted disk!\n"); return -1; } // ???????????? return 0; }
static int load_zone (int level, zone_nr * z_start, zone_nr * z_end) { int err; for (zone_nr * z = z_start; z < z_end; z++) { if (level == 0) { // FIXME: image can be > 64K err = disk_read (0, (*z) << 1, 2, i_now ? f_pos : d_dir + f_pos, i_now ? LOADSEG : seg_data ()); f_pos += BLOCK_SIZE; if (f_pos >= i_data->i_size) break; } else { int next = level - 1; err = disk_read (0, *z << 1, 2, z_block [next], seg_data ()); load_zone (next, (zone_nr *) z_block [next], (zone_nr *) (z_block [next] + BLOCK_SIZE)); } } return err; }
DRESULT disk_read ( BYTE drv, /* Physical drive nmuber (0..) */ BYTE *buff, /* Data buffer to store read data */ DWORD sector, /* Sector address (LBA) */ BYTE count /* Number of sectors to read (1..255) */ ) { SD_Error Status; #ifdef DBGIO printf("disk_read %d %p %10d %d\n",drv,buff,sector,count); #endif if (SD_Detect() != SD_PRESENT) return(RES_NOTRDY); if ((DWORD)buff & 3) // DMA Alignment failure, do single up to aligned buffer { DRESULT res = RES_OK; DWORD scratch[BLOCK_SIZE / 4]; // Alignment assured, you'll need a sufficiently big stack while(count--) { res = disk_read(drv, (void *)scratch, sector++, 1); if (res != RES_OK) break; memcpy(buff, scratch, BLOCK_SIZE); buff += BLOCK_SIZE; } return(res); } Status = SD_ReadMultiBlocksFIXED(buff, sector, BLOCK_SIZE, count); // 4GB Compliant if (Status == SD_OK) { SDTransferState State; Status = SD_WaitReadOperation(); // Check if the Transfer is finished while((State = SD_GetStatus()) == SD_TRANSFER_BUSY); // BUSY, OK (DONE), ERROR (FAIL) if ((State == SD_TRANSFER_ERROR) || (Status != SD_OK)) return(RES_ERROR); else return(RES_OK); } else return(RES_ERROR); }
/** * @brief Read data from the medium * @param lun : logical unit number * @param buf : Pointer to the buffer to save data * @param blk_addr : address of 1st block to be read * @param blk_len : nmber of blocks to be read * @retval Status */ int8_t FLASH_STORAGE_Read(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len) { disk_read(0, buf, blk_addr, blk_len); /* for (int i = 0; i < blk_len; i++) { if (!storage_read_block(buf + i * FLASH_BLOCK_SIZE, blk_addr + i)) { return -1; } } */ return 0; }