📄 nand_util.c
字号:
return ret;}/** * nand_get_lock_status: - query current lock state from one page of NAND * flash * * @param meminfo nand mtd instance * @param offset page address to query (muss be page aligned!) * * @return -1 in case of error * >0 lock status: * bitfield with the following combinations: * NAND_LOCK_STATUS_TIGHT: page in tight state * NAND_LOCK_STATUS_LOCK: page locked * NAND_LOCK_STATUS_UNLOCK: page unlocked * */int nand_get_lock_status(nand_info_t *meminfo, ulong offset){ int ret = 0; int chipnr; int page; struct nand_chip *this = meminfo->priv; /* select the NAND device */ chipnr = (int)(offset >> this->chip_shift); this->select_chip(meminfo, chipnr); if ((offset & (meminfo->writesize - 1)) != 0) { printf ("nand_get_lock_status: " "Start address must be beginning of " "nand page!\n"); ret = -1; goto out; } /* check the Lock Status */ page = (int)(offset >> this->page_shift); this->cmdfunc(meminfo, NAND_CMD_LOCK_STATUS, -1, page & this->pagemask); ret = this->read_byte(meminfo) & (NAND_LOCK_STATUS_TIGHT | NAND_LOCK_STATUS_LOCK | NAND_LOCK_STATUS_UNLOCK); out: /* de-select the NAND device */ this->select_chip(meminfo, -1); return ret;}/** * nand_unlock: - Unlock area of NAND pages * only one consecutive area can be unlocked at one time! * * @param meminfo nand mtd instance * @param start start byte address * @param length number of bytes to unlock (must be a multiple of * page size nand->writesize) * * @return 0 on success, -1 in case of error */int nand_unlock(nand_info_t *meminfo, ulong start, ulong length){ int ret = 0; int chipnr; int status; int page; struct nand_chip *this = meminfo->priv; printf ("nand_unlock: start: %08x, length: %d!\n", (int)start, (int)length); /* select the NAND device */ chipnr = (int)(start >> this->chip_shift); this->select_chip(meminfo, chipnr); /* check the WP bit */ this->cmdfunc(meminfo, NAND_CMD_STATUS, -1, -1); if ((this->read_byte(meminfo) & 0x80) == 0) { printf ("nand_unlock: Device is write protected!\n"); ret = -1; goto out; } if ((start & (meminfo->writesize - 1)) != 0) { printf ("nand_unlock: Start address must be beginning of " "nand page!\n"); ret = -1; goto out; } if (length == 0 || (length & (meminfo->writesize - 1)) != 0) { printf ("nand_unlock: Length must be a multiple of nand page " "size!\n"); ret = -1; goto out; } /* submit address of first page to unlock */ page = (int)(start >> this->page_shift); this->cmdfunc(meminfo, NAND_CMD_UNLOCK1, -1, page & this->pagemask); /* submit ADDRESS of LAST page to unlock */ page += (int)(length >> this->page_shift) - 1; this->cmdfunc(meminfo, NAND_CMD_UNLOCK2, -1, page & this->pagemask); /* call wait ready function */ status = this->waitfunc(meminfo, this, FL_WRITING); /* see if device thinks it succeeded */ if (status & 0x01) { /* there was an error */ ret = -1; goto out; } out: /* de-select the NAND device */ this->select_chip(meminfo, -1); return ret;}#endif/** * get_len_incl_bad * * Check if length including bad blocks fits into device. * * @param nand NAND device * @param offset offset in flash * @param length image length * @return image length including bad blocks */static size_t get_len_incl_bad (nand_info_t *nand, size_t offset, const size_t length){ size_t len_incl_bad = 0; size_t len_excl_bad = 0; size_t block_len; while (len_excl_bad < length) { block_len = nand->erasesize - (offset & (nand->erasesize - 1)); if (!nand_block_isbad (nand, offset & ~(nand->erasesize - 1))) len_excl_bad += block_len; len_incl_bad += block_len; offset += block_len; if ((offset + len_incl_bad) >= nand->size) break; } return len_incl_bad;}/** * nand_write_skip_bad: * * Write image to NAND flash. * Blocks that are marked bad are skipped and the is written to the next * block instead as long as the image is short enough to fit even after * skipping the bad blocks. * * @param nand NAND device * @param offset offset in flash * @param length buffer length * @param buf buffer to read from * @return 0 in case of success */int nand_write_skip_bad(nand_info_t *nand, size_t offset, size_t *length, u_char *buffer){ int rval; size_t left_to_write = *length; size_t len_incl_bad; u_char *p_buffer = buffer; /* Reject writes, which are not page aligned */ if ((offset & (nand->writesize - 1)) != 0 || (*length & (nand->writesize - 1)) != 0) { printf ("Attempt to write non page aligned data\n"); return -EINVAL; } len_incl_bad = get_len_incl_bad (nand, offset, *length); if ((offset + len_incl_bad) >= nand->size) { printf ("Attempt to write outside the flash area\n"); return -EINVAL; } if (len_incl_bad == *length) { rval = nand_write (nand, offset, length, buffer); if (rval != 0) { printf ("NAND write to offset %x failed %d\n", offset, rval); return rval; } } while (left_to_write > 0) { size_t block_offset = offset & (nand->erasesize - 1); size_t write_size; if (nand_block_isbad (nand, offset & ~(nand->erasesize - 1))) { printf ("Skip bad block 0x%08x\n", offset & ~(nand->erasesize - 1)); offset += nand->erasesize - block_offset; continue; } if (left_to_write < (nand->erasesize - block_offset)) write_size = left_to_write; else write_size = nand->erasesize - block_offset; rval = nand_write (nand, offset, &write_size, p_buffer); if (rval != 0) { printf ("NAND write to offset %x failed %d\n", offset, rval); *length -= left_to_write; return rval; } left_to_write -= write_size; offset += write_size; p_buffer += write_size; } return 0;}/** * nand_read_skip_bad: * * Read image from NAND flash. * Blocks that are marked bad are skipped and the next block is readen * instead as long as the image is short enough to fit even after skipping the * bad blocks. * * @param nand NAND device * @param offset offset in flash * @param length buffer length, on return holds remaining bytes to read * @param buffer buffer to write to * @return 0 in case of success */int nand_read_skip_bad(nand_info_t *nand, size_t offset, size_t *length, u_char *buffer){ int rval; size_t left_to_read = *length; size_t len_incl_bad; u_char *p_buffer = buffer; len_incl_bad = get_len_incl_bad (nand, offset, *length); if ((offset + len_incl_bad) >= nand->size) { printf ("Attempt to read outside the flash area\n"); return -EINVAL; } if (len_incl_bad == *length) { rval = nand_read (nand, offset, length, buffer); if (rval != 0) { printf ("NAND read from offset %x failed %d\n", offset, rval); return rval; } } while (left_to_read > 0) { size_t block_offset = offset & (nand->erasesize - 1); size_t read_length; if (nand_block_isbad (nand, offset & ~(nand->erasesize - 1))) { printf ("Skipping bad block 0x%08x\n", offset & ~(nand->erasesize - 1)); offset += nand->erasesize - block_offset; continue; } if (left_to_read < (nand->erasesize - block_offset)) read_length = left_to_read; else read_length = nand->erasesize - block_offset; rval = nand_read (nand, offset, &read_length, p_buffer); if (rval != 0) { printf ("NAND read from offset %x failed %d\n", offset, rval); *length -= left_to_read; return rval; } left_to_read -= read_length; offset += read_length; p_buffer += read_length; } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -