?? nand_util.c
字號:
* @param mtd 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(struct mtd_info *mtd, ulong offset){ int ret = 0; int chipnr; int page; struct nand_chip *chip = mtd->priv; /* select the NAND device */ chipnr = (int)(offset >> chip->chip_shift); chip->select_chip(mtd, chipnr); if ((offset & (mtd->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 >> chip->page_shift); chip->cmdfunc(mtd, NAND_CMD_LOCK_STATUS, -1, page & chip->pagemask); ret = chip->read_byte(mtd) & (NAND_LOCK_STATUS_TIGHT | NAND_LOCK_STATUS_LOCK | NAND_LOCK_STATUS_UNLOCK); out: /* de-select the NAND device */ chip->select_chip(mtd, -1); return ret;}/** * nand_unlock: - Unlock area of NAND pages * only one consecutive area can be unlocked at one time! * * @param mtd 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(struct mtd_info *mtd, ulong start, ulong length){ int ret = 0; int chipnr; int status; int page; struct nand_chip *chip = mtd->priv; printf ("nand_unlock: start: %08x, length: %d!\n", (int)start, (int)length); /* select the NAND device */ chipnr = (int)(start >> chip->chip_shift); chip->select_chip(mtd, chipnr); /* check the WP bit */ chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); if (!(chip->read_byte(mtd) & NAND_STATUS_WP)) { printf ("nand_unlock: Device is write protected!\n"); ret = -1; goto out; } if ((start & (mtd->erasesize - 1)) != 0) { printf ("nand_unlock: Start address must be beginning of " "nand block!\n"); ret = -1; goto out; } if (length == 0 || (length & (mtd->erasesize - 1)) != 0) { printf ("nand_unlock: Length must be a multiple of nand block " "size %08x!\n", mtd->erasesize); ret = -1; goto out; } /* * Set length so that the last address is set to the * starting address of the last block */ length -= mtd->erasesize; /* submit address of first page to unlock */ page = (int)(start >> chip->page_shift); chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask); /* submit ADDRESS of LAST page to unlock */ page += (int)(length >> chip->page_shift); chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1, page & chip->pagemask); /* call wait ready function */ status = chip->waitfunc(mtd, chip); /* see if device thinks it succeeded */ if (status & 0x01) { /* there was an error */ ret = -1; goto out; } out: /* de-select the NAND device */ chip->select_chip(mtd, -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 %zx 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%08zx\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 %zx 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 %zx 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%08zx\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 %zx 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 + -