blob: 1bd03b6bfa74b5b6bbc6ab78534162564f083ac8 [file] [log] [blame]
Ajay Dudani168f6cb2009-12-07 19:04:02 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
Duy Truongf3ac7b32013-02-13 01:07:28 -08004 * Copyright (c) 2009, The Linux Foundation. All rights reserved.
Ajay Dudani168f6cb2009-12-07 19:04:02 -08005 *
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>
David Ng6e1711f2010-01-19 15:27:00 -080038#include <smem.h>
Greg Griscod6250552011-06-29 14:40:23 -070039#include <platform.h>
Ajay Dudani168f6cb2009-12-07 19:04:02 -080040#include "bootimg.h"
41
42#define FLASH_PAGE_SIZE 2048
43#define FLASH_PAGE_BITS 11
44
45unsigned page_size = 0;
46unsigned page_mask = 0;
47
David Ng6e1711f2010-01-19 15:27:00 -080048static unsigned load_addr = 0xffffffff;
49
Ajay Dudani168f6cb2009-12-07 19:04:02 -080050#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
51
52void acpu_clock_init(void);
53
54int startswith(const char *str, const char *prefix)
55{
56 while(*prefix){
Greg Griscod6250552011-06-29 14:40:23 -070057 if (*prefix++ != *str++) return 0;
Ajay Dudani168f6cb2009-12-07 19:04:02 -080058 }
59 return 1;
60}
61
62/* XXX */
63void verify_flash(struct ptentry *p, void *addr, unsigned len, int extra)
64{
65 uint32_t offset = 0;
66 void *buf = malloc(FLASH_PAGE_SIZE + extra);
67 int verify_extra = extra;
68 if(verify_extra > 4)
69 verify_extra = 16;
70 while(len > 0) {
71 flash_read_ext(p, extra, offset, buf, FLASH_PAGE_SIZE);
72 if(memcmp(addr, buf, FLASH_PAGE_SIZE + verify_extra)) {
73 dprintf(CRITICAL, "verify failed at 0x%08x\n", offset);
74 jtag_fail("verify failed");
75 return;
76 }
77 offset += FLASH_PAGE_SIZE;
78 addr += FLASH_PAGE_SIZE;
79 len -= FLASH_PAGE_SIZE;
80 if(extra) {
81 addr += extra;
82 len -= extra;
83 }
84 }
85 dprintf(INFO, "verify done %d extra bytes\n", verify_extra);
86 jtag_okay("verify done");
87}
88
89void handle_flash(const char *name, unsigned addr, unsigned sz)
90{
91 struct ptentry *ptn;
92 struct ptable *ptable;
93 void *data = (void *) addr;
94 unsigned extra = 0;
95
96 ptable = flash_get_ptable();
97 if (ptable == NULL) {
98 jtag_fail("partition table doesn't exist");
99 return;
100 }
101
102 ptn = ptable_find(ptable, name);
103 if (ptn == NULL) {
104 jtag_fail("unknown partition name");
105 return;
106 }
107
108 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
109 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
110 jtag_fail("image is not a boot image");
111 return;
112 }
113 }
114
repo sync67e957b2010-06-28 16:53:45 -0700115 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata") || !strcmp(ptn->name, "persist"))
Ajay Dudani168f6cb2009-12-07 19:04:02 -0800116 extra = ((page_size >> 9) * 16);
117 else
118 sz = ROUND_TO_PAGE(sz, page_mask);
119
Ajay Dudanib5f9e502010-05-26 15:14:24 -0700120 data = (void *)target_get_scratch_address();
David Ng6e1711f2010-01-19 15:27:00 -0800121
Ajay Dudani168f6cb2009-12-07 19:04:02 -0800122 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
123 if (flash_write(ptn, extra, data, sz)) {
124 jtag_fail("flash write failure");
125 return;
126 }
127 dprintf(INFO, "partition '%s' updated\n", ptn->name);
128 jtag_okay("Done");
Chandan Uddaraju56228922009-12-11 21:48:48 -0800129 enter_critical_section();
130 platform_uninit_timer();
131 arch_disable_cache(UCACHE);
132 arch_disable_mmu();
Ajay Dudani168f6cb2009-12-07 19:04:02 -0800133}
134
135static unsigned char *tmpbuf = 0;
136
137
138/*XXX*/
139void handle_dump(const char *name, unsigned offset)
140{
141 struct ptentry *p;
142 struct ptable *ptable;
143
144 if(tmpbuf == 0) {
145 tmpbuf = malloc(4096);
146 }
147
148 dprintf(INFO, "dump '%s' partition\n", name);
149
150 ptable = flash_get_ptable();
151
152 if (ptable == NULL) {
153 jtag_fail("partition table doesn't exist");
154 return;
155 }
156
157 p = ptable_find(ptable, name);
158
159 if(p == 0) {
160 jtag_fail("partition not found");
161 return;
162 } else {
163
164#if 0
165 /* XXX reimpl */
166 if(flash_read_page(p->start * 64, tmpbuf, tmpbuf + 2048)){
167 jtag_fail("flash_read() failed");
168 return;
169 }
170#endif
171 dprintf(INFO, "page %d data:\n", p->start * 64);
172 hexdump(tmpbuf, 256);
173 dprintf(INFO, "page %d extra:\n", p->start * 64);
174 hexdump(tmpbuf, 16);
175 jtag_okay("done");
Chandan Uddaraju56228922009-12-11 21:48:48 -0800176 enter_critical_section();
177 platform_uninit_timer();
178 arch_disable_cache(UCACHE);
179 arch_disable_mmu();
Ajay Dudani168f6cb2009-12-07 19:04:02 -0800180 }
181}
182
David Ng6e1711f2010-01-19 15:27:00 -0800183void handle_query_load_address(unsigned addr)
184{
185 unsigned *return_addr = (unsigned *)addr;
186
187 if (return_addr)
Ajay Dudanib5f9e502010-05-26 15:14:24 -0700188 *return_addr = target_get_scratch_address();
David Ng6e1711f2010-01-19 15:27:00 -0800189
190 jtag_okay("done");
191}
192
Ajay Dudani168f6cb2009-12-07 19:04:02 -0800193void handle_command(const char *cmd, unsigned a0, unsigned a1, unsigned a2)
194{
195 if(startswith(cmd,"flash:")){
196 handle_flash(cmd + 6, a0, a1);
197 return;
198 }
199
200 if(startswith(cmd,"dump:")){
201 handle_dump(cmd + 5, a0);
202 return;
203 }
204
David Ng6e1711f2010-01-19 15:27:00 -0800205 if(startswith(cmd,"loadaddr:")){
206 handle_query_load_address(a0);
207 return;
208 }
209
Ajay Dudani168f6cb2009-12-07 19:04:02 -0800210 jtag_fail("unknown command");
211}
212
Chandan Uddarajubbec2b02009-12-16 13:27:55 -0800213void nandwrite_init(void)
Ajay Dudani168f6cb2009-12-07 19:04:02 -0800214{
215 page_size = flash_page_size();
216 page_mask = page_size - 1;
217 jtag_cmd_loop(handle_command);
218}
219
Ajay Dudani168f6cb2009-12-07 19:04:02 -0800220