Ajay Dudani | 168f6cb | 2009-12-07 19:04:02 -0800 | [diff] [blame^] | 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 | |
| 30 | #include <app.h> |
| 31 | #include <debug.h> |
| 32 | #include <lib/ptable.h> |
| 33 | #include <malloc.h> |
| 34 | #include <dev/flash.h> |
| 35 | #include <string.h> |
| 36 | #include <jtag.h> |
| 37 | #include "bootimg.h" |
| 38 | |
| 39 | #define FLASH_PAGE_SIZE 2048 |
| 40 | #define FLASH_PAGE_BITS 11 |
| 41 | |
| 42 | unsigned page_size = 0; |
| 43 | unsigned page_mask = 0; |
| 44 | |
| 45 | #define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y))) |
| 46 | |
| 47 | void acpu_clock_init(void); |
| 48 | |
| 49 | int startswith(const char *str, const char *prefix) |
| 50 | { |
| 51 | while(*prefix){ |
| 52 | if(*prefix++ != *str++) return 0; |
| 53 | } |
| 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | /* XXX */ |
| 58 | void verify_flash(struct ptentry *p, void *addr, unsigned len, int extra) |
| 59 | { |
| 60 | uint32_t offset = 0; |
| 61 | void *buf = malloc(FLASH_PAGE_SIZE + extra); |
| 62 | int verify_extra = extra; |
| 63 | if(verify_extra > 4) |
| 64 | verify_extra = 16; |
| 65 | while(len > 0) { |
| 66 | flash_read_ext(p, extra, offset, buf, FLASH_PAGE_SIZE); |
| 67 | if(memcmp(addr, buf, FLASH_PAGE_SIZE + verify_extra)) { |
| 68 | dprintf(CRITICAL, "verify failed at 0x%08x\n", offset); |
| 69 | jtag_fail("verify failed"); |
| 70 | return; |
| 71 | } |
| 72 | offset += FLASH_PAGE_SIZE; |
| 73 | addr += FLASH_PAGE_SIZE; |
| 74 | len -= FLASH_PAGE_SIZE; |
| 75 | if(extra) { |
| 76 | addr += extra; |
| 77 | len -= extra; |
| 78 | } |
| 79 | } |
| 80 | dprintf(INFO, "verify done %d extra bytes\n", verify_extra); |
| 81 | jtag_okay("verify done"); |
| 82 | } |
| 83 | |
| 84 | void handle_flash(const char *name, unsigned addr, unsigned sz) |
| 85 | { |
| 86 | struct ptentry *ptn; |
| 87 | struct ptable *ptable; |
| 88 | void *data = (void *) addr; |
| 89 | unsigned extra = 0; |
| 90 | |
| 91 | ptable = flash_get_ptable(); |
| 92 | if (ptable == NULL) { |
| 93 | jtag_fail("partition table doesn't exist"); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | ptn = ptable_find(ptable, name); |
| 98 | if (ptn == NULL) { |
| 99 | jtag_fail("unknown partition name"); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) { |
| 104 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 105 | jtag_fail("image is not a boot image"); |
| 106 | return; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")) |
| 111 | extra = ((page_size >> 9) * 16); |
| 112 | else |
| 113 | sz = ROUND_TO_PAGE(sz, page_mask); |
| 114 | |
| 115 | dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name); |
| 116 | if (flash_write(ptn, extra, data, sz)) { |
| 117 | jtag_fail("flash write failure"); |
| 118 | return; |
| 119 | } |
| 120 | dprintf(INFO, "partition '%s' updated\n", ptn->name); |
| 121 | jtag_okay("Done"); |
| 122 | } |
| 123 | |
| 124 | static unsigned char *tmpbuf = 0; |
| 125 | |
| 126 | |
| 127 | /*XXX*/ |
| 128 | void handle_dump(const char *name, unsigned offset) |
| 129 | { |
| 130 | struct ptentry *p; |
| 131 | struct ptable *ptable; |
| 132 | |
| 133 | if(tmpbuf == 0) { |
| 134 | tmpbuf = malloc(4096); |
| 135 | } |
| 136 | |
| 137 | dprintf(INFO, "dump '%s' partition\n", name); |
| 138 | |
| 139 | ptable = flash_get_ptable(); |
| 140 | |
| 141 | if (ptable == NULL) { |
| 142 | jtag_fail("partition table doesn't exist"); |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | p = ptable_find(ptable, name); |
| 147 | |
| 148 | if(p == 0) { |
| 149 | jtag_fail("partition not found"); |
| 150 | return; |
| 151 | } else { |
| 152 | |
| 153 | #if 0 |
| 154 | /* XXX reimpl */ |
| 155 | if(flash_read_page(p->start * 64, tmpbuf, tmpbuf + 2048)){ |
| 156 | jtag_fail("flash_read() failed"); |
| 157 | return; |
| 158 | } |
| 159 | #endif |
| 160 | dprintf(INFO, "page %d data:\n", p->start * 64); |
| 161 | hexdump(tmpbuf, 256); |
| 162 | dprintf(INFO, "page %d extra:\n", p->start * 64); |
| 163 | hexdump(tmpbuf, 16); |
| 164 | jtag_okay("done"); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void handle_command(const char *cmd, unsigned a0, unsigned a1, unsigned a2) |
| 169 | { |
| 170 | if(startswith(cmd,"flash:")){ |
| 171 | handle_flash(cmd + 6, a0, a1); |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | if(startswith(cmd,"dump:")){ |
| 176 | handle_dump(cmd + 5, a0); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | jtag_fail("unknown command"); |
| 181 | } |
| 182 | |
| 183 | void nandwrite_init(const struct app_descriptor *app) |
| 184 | { |
| 185 | page_size = flash_page_size(); |
| 186 | page_mask = page_size - 1; |
| 187 | jtag_cmd_loop(handle_command); |
| 188 | } |
| 189 | |
| 190 | APP_START(nandwrite) |
| 191 | .init = nandwrite_init, |
| 192 | APP_END |
| 193 | |
| 194 | |