blob: d37011b721e1f8304205c68709e5244db3ca5dd9 [file] [log] [blame]
Ajay Dudani168f6cb2009-12-07 19:04:02 -08001/*
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 Uddaraju56228922009-12-11 21:48:48 -080037#include <kernel/thread.h>
Ajay Dudani168f6cb2009-12-07 19:04:02 -080038#include "bootimg.h"
39
40#define FLASH_PAGE_SIZE 2048
41#define FLASH_PAGE_BITS 11
42
43unsigned page_size = 0;
44unsigned page_mask = 0;
45
46#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
47
48void acpu_clock_init(void);
Chandan Uddaraju56228922009-12-11 21:48:48 -080049void platform_uninit_timer(void);
Ajay Dudani168f6cb2009-12-07 19:04:02 -080050
51int 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 */
60void 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
86void 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 Uddaraju56228922009-12-11 21:48:48 -0800124 enter_critical_section();
125 platform_uninit_timer();
126 arch_disable_cache(UCACHE);
127 arch_disable_mmu();
Ajay Dudani168f6cb2009-12-07 19:04:02 -0800128}
129
130static unsigned char *tmpbuf = 0;
131
132
133/*XXX*/
134void 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 Uddaraju56228922009-12-11 21:48:48 -0800171 enter_critical_section();
172 platform_uninit_timer();
173 arch_disable_cache(UCACHE);
174 arch_disable_mmu();
Ajay Dudani168f6cb2009-12-07 19:04:02 -0800175 }
176}
177
178void 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
193void 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
200APP_START(nandwrite)
201 .init = nandwrite_init,
202APP_END
203
204