nand_util.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /*
  2. * drivers/mtd/nand/nand_util.c
  3. *
  4. * Copyright (C) 2006 by Weiss-Electronic GmbH.
  5. * All rights reserved.
  6. *
  7. * @author: Guido Classen <clagix@gmail.com>
  8. * @descr: NAND Flash support
  9. * @references: borrowed heavily from Linux mtd-utils code:
  10. * flash_eraseall.c by Arcom Control System Ltd
  11. * nandwrite.c by Steven J. Hill (sjhill@realitydiluted.com)
  12. * and Thomas Gleixner (tglx@linutronix.de)
  13. *
  14. * Copyright (C) 2008 Nokia Corporation: drop_ffs() function by
  15. * Artem Bityutskiy <dedekind1@gmail.com> from mtd-utils
  16. *
  17. * See file CREDITS for list of people who contributed to this
  18. * project.
  19. *
  20. * This program is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU General Public License version
  22. * 2 as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  32. * MA 02111-1307 USA
  33. *
  34. * Copyright 2010 Freescale Semiconductor
  35. * The portions of this file whose copyright is held by Freescale and which
  36. * are not considered a derived work of GPL v2-only code may be distributed
  37. * and/or modified under the terms of the GNU General Public License as
  38. * published by the Free Software Foundation; either version 2 of the
  39. * License, or (at your option) any later version.
  40. */
  41. #include <common.h>
  42. #include <command.h>
  43. #include <watchdog.h>
  44. #include <malloc.h>
  45. #include <div64.h>
  46. #include <asm/errno.h>
  47. #include <linux/mtd/mtd.h>
  48. #include <nand.h>
  49. #include <jffs2/jffs2.h>
  50. typedef struct erase_info erase_info_t;
  51. typedef struct mtd_info mtd_info_t;
  52. /* support only for native endian JFFS2 */
  53. #define cpu_to_je16(x) (x)
  54. #define cpu_to_je32(x) (x)
  55. /*****************************************************************************/
  56. static int nand_block_bad_scrub(struct mtd_info *mtd, loff_t ofs, int getchip)
  57. {
  58. return 0;
  59. }
  60. /**
  61. * nand_erase_opts: - erase NAND flash with support for various options
  62. * (jffs2 formating)
  63. *
  64. * @param meminfo NAND device to erase
  65. * @param opts options, @see struct nand_erase_options
  66. * @return 0 in case of success
  67. *
  68. * This code is ported from flash_eraseall.c from Linux mtd utils by
  69. * Arcom Control System Ltd.
  70. */
  71. int nand_erase_opts(nand_info_t *meminfo, const nand_erase_options_t *opts)
  72. {
  73. struct jffs2_unknown_node cleanmarker;
  74. erase_info_t erase;
  75. unsigned long erase_length, erased_length; /* in blocks */
  76. int bbtest = 1;
  77. int result;
  78. int percent_complete = -1;
  79. int (*nand_block_bad_old)(struct mtd_info *, loff_t, int) = NULL;
  80. const char *mtd_device = meminfo->name;
  81. struct mtd_oob_ops oob_opts;
  82. struct nand_chip *chip = meminfo->priv;
  83. if ((opts->offset & (meminfo->writesize - 1)) != 0) {
  84. printf("Attempt to erase non page aligned data\n");
  85. return -1;
  86. }
  87. memset(&erase, 0, sizeof(erase));
  88. memset(&oob_opts, 0, sizeof(oob_opts));
  89. erase.mtd = meminfo;
  90. erase.len = meminfo->erasesize;
  91. erase.addr = opts->offset;
  92. erase_length = lldiv(opts->length + meminfo->erasesize - 1,
  93. meminfo->erasesize);
  94. cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
  95. cleanmarker.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER);
  96. cleanmarker.totlen = cpu_to_je32(8);
  97. /* scrub option allows to erase badblock. To prevent internal
  98. * check from erase() method, set block check method to dummy
  99. * and disable bad block table while erasing.
  100. */
  101. if (opts->scrub) {
  102. struct nand_chip *priv_nand = meminfo->priv;
  103. nand_block_bad_old = priv_nand->block_bad;
  104. priv_nand->block_bad = nand_block_bad_scrub;
  105. /* we don't need the bad block table anymore...
  106. * after scrub, there are no bad blocks left!
  107. */
  108. if (priv_nand->bbt) {
  109. kfree(priv_nand->bbt);
  110. }
  111. priv_nand->bbt = NULL;
  112. }
  113. for (erased_length = 0;
  114. erased_length < erase_length;
  115. erase.addr += meminfo->erasesize) {
  116. WATCHDOG_RESET ();
  117. if (!opts->scrub && bbtest) {
  118. int ret = meminfo->block_isbad(meminfo, erase.addr);
  119. if (ret > 0) {
  120. if (!opts->quiet)
  121. printf("\rSkipping bad block at "
  122. "0x%08llx "
  123. " \n",
  124. erase.addr);
  125. if (!opts->spread)
  126. erased_length++;
  127. continue;
  128. } else if (ret < 0) {
  129. printf("\n%s: MTD get bad block failed: %d\n",
  130. mtd_device,
  131. ret);
  132. return -1;
  133. }
  134. }
  135. erased_length++;
  136. result = meminfo->erase(meminfo, &erase);
  137. if (result != 0) {
  138. printf("\n%s: MTD Erase failure: %d\n",
  139. mtd_device, result);
  140. continue;
  141. }
  142. /* format for JFFS2 ? */
  143. if (opts->jffs2 && chip->ecc.layout->oobavail >= 8) {
  144. chip->ops.ooblen = 8;
  145. chip->ops.datbuf = NULL;
  146. chip->ops.oobbuf = (uint8_t *)&cleanmarker;
  147. chip->ops.ooboffs = 0;
  148. chip->ops.mode = MTD_OOB_AUTO;
  149. result = meminfo->write_oob(meminfo,
  150. erase.addr,
  151. &chip->ops);
  152. if (result != 0) {
  153. printf("\n%s: MTD writeoob failure: %d\n",
  154. mtd_device, result);
  155. continue;
  156. }
  157. }
  158. if (!opts->quiet) {
  159. unsigned long long n = erased_length * 100ULL;
  160. int percent;
  161. do_div(n, erase_length);
  162. percent = (int)n;
  163. /* output progress message only at whole percent
  164. * steps to reduce the number of messages printed
  165. * on (slow) serial consoles
  166. */
  167. if (percent != percent_complete) {
  168. percent_complete = percent;
  169. printf("\rErasing at 0x%llx -- %3d%% complete.",
  170. erase.addr, percent);
  171. if (opts->jffs2 && result == 0)
  172. printf(" Cleanmarker written at 0x%llx.",
  173. erase.addr);
  174. }
  175. }
  176. }
  177. if (!opts->quiet)
  178. printf("\n");
  179. if (nand_block_bad_old) {
  180. struct nand_chip *priv_nand = meminfo->priv;
  181. priv_nand->block_bad = nand_block_bad_old;
  182. priv_nand->scan_bbt(meminfo);
  183. }
  184. return 0;
  185. }
  186. #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
  187. /******************************************************************************
  188. * Support for locking / unlocking operations of some NAND devices
  189. *****************************************************************************/
  190. #define NAND_CMD_LOCK 0x2a
  191. #define NAND_CMD_LOCK_TIGHT 0x2c
  192. #define NAND_CMD_UNLOCK1 0x23
  193. #define NAND_CMD_UNLOCK2 0x24
  194. #define NAND_CMD_LOCK_STATUS 0x7a
  195. /**
  196. * nand_lock: Set all pages of NAND flash chip to the LOCK or LOCK-TIGHT
  197. * state
  198. *
  199. * @param mtd nand mtd instance
  200. * @param tight bring device in lock tight mode
  201. *
  202. * @return 0 on success, -1 in case of error
  203. *
  204. * The lock / lock-tight command only applies to the whole chip. To get some
  205. * parts of the chip lock and others unlocked use the following sequence:
  206. *
  207. * - Lock all pages of the chip using nand_lock(mtd, 0) (or the lockpre pin)
  208. * - Call nand_unlock() once for each consecutive area to be unlocked
  209. * - If desired: Bring the chip to the lock-tight state using nand_lock(mtd, 1)
  210. *
  211. * If the device is in lock-tight state software can't change the
  212. * current active lock/unlock state of all pages. nand_lock() / nand_unlock()
  213. * calls will fail. It is only posible to leave lock-tight state by
  214. * an hardware signal (low pulse on _WP pin) or by power down.
  215. */
  216. int nand_lock(struct mtd_info *mtd, int tight)
  217. {
  218. int ret = 0;
  219. int status;
  220. struct nand_chip *chip = mtd->priv;
  221. /* select the NAND device */
  222. chip->select_chip(mtd, 0);
  223. chip->cmdfunc(mtd,
  224. (tight ? NAND_CMD_LOCK_TIGHT : NAND_CMD_LOCK),
  225. -1, -1);
  226. /* call wait ready function */
  227. status = chip->waitfunc(mtd, chip);
  228. /* see if device thinks it succeeded */
  229. if (status & 0x01) {
  230. ret = -1;
  231. }
  232. /* de-select the NAND device */
  233. chip->select_chip(mtd, -1);
  234. return ret;
  235. }
  236. /**
  237. * nand_get_lock_status: - query current lock state from one page of NAND
  238. * flash
  239. *
  240. * @param mtd nand mtd instance
  241. * @param offset page address to query (muss be page aligned!)
  242. *
  243. * @return -1 in case of error
  244. * >0 lock status:
  245. * bitfield with the following combinations:
  246. * NAND_LOCK_STATUS_TIGHT: page in tight state
  247. * NAND_LOCK_STATUS_LOCK: page locked
  248. * NAND_LOCK_STATUS_UNLOCK: page unlocked
  249. *
  250. */
  251. int nand_get_lock_status(struct mtd_info *mtd, loff_t offset)
  252. {
  253. int ret = 0;
  254. int chipnr;
  255. int page;
  256. struct nand_chip *chip = mtd->priv;
  257. /* select the NAND device */
  258. chipnr = (int)(offset >> chip->chip_shift);
  259. chip->select_chip(mtd, chipnr);
  260. if ((offset & (mtd->writesize - 1)) != 0) {
  261. printf ("nand_get_lock_status: "
  262. "Start address must be beginning of "
  263. "nand page!\n");
  264. ret = -1;
  265. goto out;
  266. }
  267. /* check the Lock Status */
  268. page = (int)(offset >> chip->page_shift);
  269. chip->cmdfunc(mtd, NAND_CMD_LOCK_STATUS, -1, page & chip->pagemask);
  270. ret = chip->read_byte(mtd) & (NAND_LOCK_STATUS_TIGHT
  271. | NAND_LOCK_STATUS_LOCK
  272. | NAND_LOCK_STATUS_UNLOCK);
  273. out:
  274. /* de-select the NAND device */
  275. chip->select_chip(mtd, -1);
  276. return ret;
  277. }
  278. /**
  279. * nand_unlock: - Unlock area of NAND pages
  280. * only one consecutive area can be unlocked at one time!
  281. *
  282. * @param mtd nand mtd instance
  283. * @param start start byte address
  284. * @param length number of bytes to unlock (must be a multiple of
  285. * page size nand->writesize)
  286. *
  287. * @return 0 on success, -1 in case of error
  288. */
  289. int nand_unlock(struct mtd_info *mtd, ulong start, ulong length)
  290. {
  291. int ret = 0;
  292. int chipnr;
  293. int status;
  294. int page;
  295. struct nand_chip *chip = mtd->priv;
  296. printf ("nand_unlock: start: %08x, length: %d!\n",
  297. (int)start, (int)length);
  298. /* select the NAND device */
  299. chipnr = (int)(start >> chip->chip_shift);
  300. chip->select_chip(mtd, chipnr);
  301. /* check the WP bit */
  302. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  303. if (!(chip->read_byte(mtd) & NAND_STATUS_WP)) {
  304. printf ("nand_unlock: Device is write protected!\n");
  305. ret = -1;
  306. goto out;
  307. }
  308. if ((start & (mtd->erasesize - 1)) != 0) {
  309. printf ("nand_unlock: Start address must be beginning of "
  310. "nand block!\n");
  311. ret = -1;
  312. goto out;
  313. }
  314. if (length == 0 || (length & (mtd->erasesize - 1)) != 0) {
  315. printf ("nand_unlock: Length must be a multiple of nand block "
  316. "size %08x!\n", mtd->erasesize);
  317. ret = -1;
  318. goto out;
  319. }
  320. /*
  321. * Set length so that the last address is set to the
  322. * starting address of the last block
  323. */
  324. length -= mtd->erasesize;
  325. /* submit address of first page to unlock */
  326. page = (int)(start >> chip->page_shift);
  327. chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
  328. /* submit ADDRESS of LAST page to unlock */
  329. page += (int)(length >> chip->page_shift);
  330. chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1, page & chip->pagemask);
  331. /* call wait ready function */
  332. status = chip->waitfunc(mtd, chip);
  333. /* see if device thinks it succeeded */
  334. if (status & 0x01) {
  335. /* there was an error */
  336. ret = -1;
  337. goto out;
  338. }
  339. out:
  340. /* de-select the NAND device */
  341. chip->select_chip(mtd, -1);
  342. return ret;
  343. }
  344. #endif
  345. /**
  346. * check_skip_len
  347. *
  348. * Check if there are any bad blocks, and whether length including bad
  349. * blocks fits into device
  350. *
  351. * @param nand NAND device
  352. * @param offset offset in flash
  353. * @param length image length
  354. * @return 0 if the image fits and there are no bad blocks
  355. * 1 if the image fits, but there are bad blocks
  356. * -1 if the image does not fit
  357. */
  358. static int check_skip_len(nand_info_t *nand, loff_t offset, size_t length)
  359. {
  360. size_t len_excl_bad = 0;
  361. int ret = 0;
  362. while (len_excl_bad < length) {
  363. size_t block_len, block_off;
  364. loff_t block_start;
  365. if (offset >= nand->size)
  366. return -1;
  367. block_start = offset & ~(loff_t)(nand->erasesize - 1);
  368. block_off = offset & (nand->erasesize - 1);
  369. block_len = nand->erasesize - block_off;
  370. if (!nand_block_isbad(nand, block_start))
  371. len_excl_bad += block_len;
  372. else
  373. ret = 1;
  374. offset += block_len;
  375. }
  376. return ret;
  377. }
  378. #ifdef CONFIG_CMD_NAND_TRIMFFS
  379. static size_t drop_ffs(const nand_info_t *nand, const u_char *buf,
  380. const size_t *len)
  381. {
  382. size_t i, l = *len;
  383. for (i = l - 1; i >= 0; i--)
  384. if (buf[i] != 0xFF)
  385. break;
  386. /* The resulting length must be aligned to the minimum flash I/O size */
  387. l = i + 1;
  388. l = (l + nand->writesize - 1) / nand->writesize;
  389. l *= nand->writesize;
  390. /*
  391. * since the input length may be unaligned, prevent access past the end
  392. * of the buffer
  393. */
  394. return min(l, *len);
  395. }
  396. #endif
  397. /**
  398. * nand_write_skip_bad:
  399. *
  400. * Write image to NAND flash.
  401. * Blocks that are marked bad are skipped and the is written to the next
  402. * block instead as long as the image is short enough to fit even after
  403. * skipping the bad blocks.
  404. *
  405. * @param nand NAND device
  406. * @param offset offset in flash
  407. * @param length buffer length
  408. * @param buffer buffer to read from
  409. * @param flags flags modifying the behaviour of the write to NAND
  410. * @return 0 in case of success
  411. */
  412. int nand_write_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
  413. u_char *buffer, int flags)
  414. {
  415. int rval = 0, blocksize;
  416. size_t left_to_write = *length;
  417. u_char *p_buffer = buffer;
  418. int need_skip;
  419. #ifdef CONFIG_CMD_NAND_YAFFS
  420. if (flags & WITH_YAFFS_OOB) {
  421. if (flags & ~WITH_YAFFS_OOB)
  422. return -EINVAL;
  423. int pages;
  424. pages = nand->erasesize / nand->writesize;
  425. blocksize = (pages * nand->oobsize) + nand->erasesize;
  426. if (*length % (nand->writesize + nand->oobsize)) {
  427. printf ("Attempt to write incomplete page"
  428. " in yaffs mode\n");
  429. return -EINVAL;
  430. }
  431. } else
  432. #endif
  433. {
  434. blocksize = nand->erasesize;
  435. }
  436. /*
  437. * nand_write() handles unaligned, partial page writes.
  438. *
  439. * We allow length to be unaligned, for convenience in
  440. * using the $filesize variable.
  441. *
  442. * However, starting at an unaligned offset makes the
  443. * semantics of bad block skipping ambiguous (really,
  444. * you should only start a block skipping access at a
  445. * partition boundary). So don't try to handle that.
  446. */
  447. if ((offset & (nand->writesize - 1)) != 0) {
  448. printf ("Attempt to write non page aligned data\n");
  449. *length = 0;
  450. return -EINVAL;
  451. }
  452. need_skip = check_skip_len(nand, offset, *length);
  453. if (need_skip < 0) {
  454. printf ("Attempt to write outside the flash area\n");
  455. *length = 0;
  456. return -EINVAL;
  457. }
  458. if (!need_skip && !(flags & WITH_DROP_FFS)) {
  459. rval = nand_write (nand, offset, length, buffer);
  460. if (rval == 0)
  461. return 0;
  462. *length = 0;
  463. printf ("NAND write to offset %llx failed %d\n",
  464. offset, rval);
  465. return rval;
  466. }
  467. while (left_to_write > 0) {
  468. size_t block_offset = offset & (nand->erasesize - 1);
  469. size_t write_size, truncated_write_size;
  470. WATCHDOG_RESET ();
  471. if (nand_block_isbad (nand, offset & ~(nand->erasesize - 1))) {
  472. printf ("Skip bad block 0x%08llx\n",
  473. offset & ~(nand->erasesize - 1));
  474. offset += nand->erasesize - block_offset;
  475. continue;
  476. }
  477. if (left_to_write < (blocksize - block_offset))
  478. write_size = left_to_write;
  479. else
  480. write_size = blocksize - block_offset;
  481. #ifdef CONFIG_CMD_NAND_YAFFS
  482. if (flags & WITH_YAFFS_OOB) {
  483. int page, pages;
  484. size_t pagesize = nand->writesize;
  485. size_t pagesize_oob = pagesize + nand->oobsize;
  486. struct mtd_oob_ops ops;
  487. ops.len = pagesize;
  488. ops.ooblen = nand->oobsize;
  489. ops.mode = MTD_OOB_AUTO;
  490. ops.ooboffs = 0;
  491. pages = write_size / pagesize_oob;
  492. for (page = 0; page < pages; page++) {
  493. WATCHDOG_RESET();
  494. ops.datbuf = p_buffer;
  495. ops.oobbuf = ops.datbuf + pagesize;
  496. rval = nand->write_oob(nand, offset, &ops);
  497. if (!rval)
  498. break;
  499. offset += pagesize;
  500. p_buffer += pagesize_oob;
  501. }
  502. }
  503. else
  504. #endif
  505. {
  506. truncated_write_size = write_size;
  507. #ifdef CONFIG_CMD_NAND_TRIMFFS
  508. if (flags & WITH_DROP_FFS)
  509. truncated_write_size = drop_ffs(nand, p_buffer,
  510. &write_size);
  511. #endif
  512. rval = nand_write(nand, offset, &truncated_write_size,
  513. p_buffer);
  514. offset += write_size;
  515. p_buffer += write_size;
  516. }
  517. if (rval != 0) {
  518. printf ("NAND write to offset %llx failed %d\n",
  519. offset, rval);
  520. *length -= left_to_write;
  521. return rval;
  522. }
  523. left_to_write -= write_size;
  524. }
  525. return 0;
  526. }
  527. /**
  528. * nand_read_skip_bad:
  529. *
  530. * Read image from NAND flash.
  531. * Blocks that are marked bad are skipped and the next block is readen
  532. * instead as long as the image is short enough to fit even after skipping the
  533. * bad blocks.
  534. *
  535. * @param nand NAND device
  536. * @param offset offset in flash
  537. * @param length buffer length, on return holds remaining bytes to read
  538. * @param buffer buffer to write to
  539. * @return 0 in case of success
  540. */
  541. int nand_read_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
  542. u_char *buffer)
  543. {
  544. int rval;
  545. size_t left_to_read = *length;
  546. u_char *p_buffer = buffer;
  547. int need_skip;
  548. if ((offset & (nand->writesize - 1)) != 0) {
  549. printf ("Attempt to read non page aligned data\n");
  550. *length = 0;
  551. return -EINVAL;
  552. }
  553. need_skip = check_skip_len(nand, offset, *length);
  554. if (need_skip < 0) {
  555. printf ("Attempt to read outside the flash area\n");
  556. *length = 0;
  557. return -EINVAL;
  558. }
  559. if (!need_skip) {
  560. rval = nand_read (nand, offset, length, buffer);
  561. if (!rval || rval == -EUCLEAN)
  562. return 0;
  563. *length = 0;
  564. printf ("NAND read from offset %llx failed %d\n",
  565. offset, rval);
  566. return rval;
  567. }
  568. while (left_to_read > 0) {
  569. size_t block_offset = offset & (nand->erasesize - 1);
  570. size_t read_length;
  571. WATCHDOG_RESET ();
  572. if (nand_block_isbad (nand, offset & ~(nand->erasesize - 1))) {
  573. printf ("Skipping bad block 0x%08llx\n",
  574. offset & ~(nand->erasesize - 1));
  575. offset += nand->erasesize - block_offset;
  576. continue;
  577. }
  578. if (left_to_read < (nand->erasesize - block_offset))
  579. read_length = left_to_read;
  580. else
  581. read_length = nand->erasesize - block_offset;
  582. rval = nand_read (nand, offset, &read_length, p_buffer);
  583. if (rval && rval != -EUCLEAN) {
  584. printf ("NAND read from offset %llx failed %d\n",
  585. offset, rval);
  586. *length -= left_to_read;
  587. return rval;
  588. }
  589. left_to_read -= read_length;
  590. offset += read_length;
  591. p_buffer += read_length;
  592. }
  593. return 0;
  594. }