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