nandwrite.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (C) 2008 The Android Open Source Project
  3. * All rights reserved.
  4. * Copyright (c) 2009, Code Aurora Forum. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  23. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  24. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  26. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <app.h>
  30. #include <debug.h>
  31. #include <lib/ptable.h>
  32. #include <malloc.h>
  33. #include <dev/flash.h>
  34. #include <string.h>
  35. #include <jtag.h>
  36. #include <kernel/thread.h>
  37. #include <smem.h>
  38. #include <platform.h>
  39. #include "bootimg.h"
  40. #define FLASH_PAGE_SIZE 2048
  41. #define FLASH_PAGE_BITS 11
  42. unsigned page_size = 0;
  43. unsigned page_mask = 0;
  44. static unsigned load_addr = 0xffffffff;
  45. #define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
  46. void acpu_clock_init(void);
  47. int startswith(const char *str, const char *prefix)
  48. {
  49. while(*prefix){
  50. if (*prefix++ != *str++) return 0;
  51. }
  52. return 1;
  53. }
  54. /* XXX */
  55. void verify_flash(struct ptentry *p, void *addr, unsigned len, int extra)
  56. {
  57. uint32_t offset = 0;
  58. void *buf = malloc(FLASH_PAGE_SIZE + extra);
  59. int verify_extra = extra;
  60. if(verify_extra > 4)
  61. verify_extra = 16;
  62. while(len > 0) {
  63. flash_read_ext(p, extra, offset, buf, FLASH_PAGE_SIZE);
  64. if(memcmp(addr, buf, FLASH_PAGE_SIZE + verify_extra)) {
  65. dprintf(CRITICAL, "verify failed at 0x%08x\n", offset);
  66. jtag_fail("verify failed");
  67. return;
  68. }
  69. offset += FLASH_PAGE_SIZE;
  70. addr += FLASH_PAGE_SIZE;
  71. len -= FLASH_PAGE_SIZE;
  72. if(extra) {
  73. addr += extra;
  74. len -= extra;
  75. }
  76. }
  77. dprintf(INFO, "verify done %d extra bytes\n", verify_extra);
  78. jtag_okay("verify done");
  79. }
  80. void handle_flash(const char *name, unsigned addr, unsigned sz)
  81. {
  82. struct ptentry *ptn;
  83. struct ptable *ptable;
  84. void *data = (void *) addr;
  85. unsigned extra = 0;
  86. ptable = flash_get_ptable();
  87. if (ptable == NULL) {
  88. jtag_fail("partition table doesn't exist");
  89. return;
  90. }
  91. ptn = ptable_find(ptable, name);
  92. if (ptn == NULL) {
  93. jtag_fail("unknown partition name");
  94. return;
  95. }
  96. if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
  97. if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
  98. jtag_fail("image is not a boot image");
  99. return;
  100. }
  101. }
  102. if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata") || !strcmp(ptn->name, "persist"))
  103. extra = ((page_size >> 9) * 16);
  104. else
  105. sz = ROUND_TO_PAGE(sz, page_mask);
  106. data = (void *)target_get_scratch_address();
  107. dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
  108. if (flash_write(ptn, extra, data, sz)) {
  109. jtag_fail("flash write failure");
  110. return;
  111. }
  112. dprintf(INFO, "partition '%s' updated\n", ptn->name);
  113. jtag_okay("Done");
  114. enter_critical_section();
  115. platform_uninit_timer();
  116. arch_disable_cache(UCACHE);
  117. arch_disable_mmu();
  118. }
  119. static unsigned char *tmpbuf = 0;
  120. /*XXX*/
  121. void handle_dump(const char *name, unsigned offset)
  122. {
  123. struct ptentry *p;
  124. struct ptable *ptable;
  125. if(tmpbuf == 0) {
  126. tmpbuf = malloc(4096);
  127. }
  128. dprintf(INFO, "dump '%s' partition\n", name);
  129. ptable = flash_get_ptable();
  130. if (ptable == NULL) {
  131. jtag_fail("partition table doesn't exist");
  132. return;
  133. }
  134. p = ptable_find(ptable, name);
  135. if(p == 0) {
  136. jtag_fail("partition not found");
  137. return;
  138. } else {
  139. #if 0
  140. /* XXX reimpl */
  141. if(flash_read_page(p->start * 64, tmpbuf, tmpbuf + 2048)){
  142. jtag_fail("flash_read() failed");
  143. return;
  144. }
  145. #endif
  146. dprintf(INFO, "page %d data:\n", p->start * 64);
  147. hexdump(tmpbuf, 256);
  148. dprintf(INFO, "page %d extra:\n", p->start * 64);
  149. hexdump(tmpbuf, 16);
  150. jtag_okay("done");
  151. enter_critical_section();
  152. platform_uninit_timer();
  153. arch_disable_cache(UCACHE);
  154. arch_disable_mmu();
  155. }
  156. }
  157. void handle_query_load_address(unsigned addr)
  158. {
  159. unsigned *return_addr = (unsigned *)addr;
  160. if (return_addr)
  161. *return_addr = target_get_scratch_address();
  162. jtag_okay("done");
  163. }
  164. void handle_command(const char *cmd, unsigned a0, unsigned a1, unsigned a2)
  165. {
  166. if(startswith(cmd,"flash:")){
  167. handle_flash(cmd + 6, a0, a1);
  168. return;
  169. }
  170. if(startswith(cmd,"dump:")){
  171. handle_dump(cmd + 5, a0);
  172. return;
  173. }
  174. if(startswith(cmd,"loadaddr:")){
  175. handle_query_load_address(a0);
  176. return;
  177. }
  178. jtag_fail("unknown command");
  179. }
  180. void nandwrite_init(void)
  181. {
  182. page_size = flash_page_size();
  183. page_mask = page_size - 1;
  184. jtag_cmd_loop(handle_command);
  185. }