blob: 898ccb6a7a4a9778a7f49600f53841b8e3938e13 [file] [log] [blame]
Greg Griscod6250552011-06-29 14:40:23 -07001/* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
Shashank Mittal024c0332010-02-03 11:44:00 -08002
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of Code Aurora Forum, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <debug.h>
30#include <arch/arm.h>
31#include <dev/udc.h>
32#include <string.h>
33#include <kernel/thread.h>
34#include <arch/ops.h>
35
36#include <dev/flash.h>
37#include <lib/ptable.h>
38#include <dev/keys.h>
Greg Griscod6250552011-06-29 14:40:23 -070039#include <platform.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070040#include <partition_parser.h>
Greg Griscod2471ef2011-07-14 13:00:42 -070041#include <mmc.h>
Shashank Mittal024c0332010-02-03 11:44:00 -080042
43#include "recovery.h"
44#include "bootimg.h"
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -070045#include "smem.h"
46
47#define BOOT_FLAGS 1
48#define UPDATE_STATUS 2
49#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Shashank Mittal024c0332010-02-03 11:44:00 -080050
51static const int MISC_PAGES = 3; // number of pages to save
52static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page
53static char buf[4096];
54unsigned boot_into_recovery = 0;
55
Shashank Mittal162244e2011-08-08 19:01:25 -070056extern void reset_device_info();
57extern void set_device_root();
Shashank Mittal024c0332010-02-03 11:44:00 -080058
59int get_recovery_message(struct recovery_message *out)
60{
61 struct ptentry *ptn;
62 struct ptable *ptable;
63 unsigned offset = 0;
64 unsigned pagesize = flash_page_size();
65
66 ptable = flash_get_ptable();
67
68 if (ptable == NULL) {
69 dprintf(CRITICAL, "ERROR: Partition table not found\n");
70 return -1;
71 }
72 ptn = ptable_find(ptable, "misc");
73
74 if (ptn == NULL) {
75 dprintf(CRITICAL, "ERROR: No misc partition found\n");
76 return -1;
77 }
78
79 offset += (pagesize * MISC_COMMAND_PAGE);
Greg Griscod6250552011-06-29 14:40:23 -070080 if (flash_read(ptn, offset, (void *) buf, pagesize)) {
Shashank Mittal024c0332010-02-03 11:44:00 -080081 dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n");
82 return -1;
83 }
84 memcpy(out, buf, sizeof(*out));
85 return 0;
86}
87
88int set_recovery_message(const struct recovery_message *in)
89{
90 struct ptentry *ptn;
91 struct ptable *ptable;
92 unsigned offset = 0;
93 unsigned pagesize = flash_page_size();
94 unsigned n = 0;
95
96 ptable = flash_get_ptable();
97
98 if (ptable == NULL) {
99 dprintf(CRITICAL, "ERROR: Partition table not found\n");
100 return -1;
101 }
102 ptn = ptable_find(ptable, "misc");
103
104 if (ptn == NULL) {
105 dprintf(CRITICAL, "ERROR: No misc partition found\n");
106 return -1;
107 }
108
109 n = pagesize * (MISC_COMMAND_PAGE + 1);
110
Greg Griscod6250552011-06-29 14:40:23 -0700111 if (flash_read(ptn, offset, (void *) SCRATCH_ADDR, n)) {
Shashank Mittal024c0332010-02-03 11:44:00 -0800112 dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n");
113 return -1;
114 }
115
116 offset += (pagesize * MISC_COMMAND_PAGE);
117 offset += SCRATCH_ADDR;
Greg Griscod6250552011-06-29 14:40:23 -0700118 memcpy((void *) offset, in, sizeof(*in));
Shashank Mittal024c0332010-02-03 11:44:00 -0800119 if (flash_write(ptn, 0, (void *)SCRATCH_ADDR, n)) {
120 dprintf(CRITICAL, "ERROR: flash write fail!\n");
121 return -1;
122 }
Greg Griscod6250552011-06-29 14:40:23 -0700123 return 0;
Shashank Mittal024c0332010-02-03 11:44:00 -0800124}
125
126int read_update_header_for_bootloader(struct update_header *header)
127{
128 struct ptentry *ptn;
129 struct ptable *ptable;
130 unsigned offset = 0;
131 unsigned pagesize = flash_page_size();
132
133 ptable = flash_get_ptable();
134 if (ptable == NULL) {
135 dprintf(CRITICAL, "ERROR: Partition table not found\n");
136 return -1;
137 }
138 ptn = ptable_find(ptable, "cache");
139
140 if (ptn == NULL) {
141 dprintf(CRITICAL, "ERROR: No cache partition found\n");
142 return -1;
143 }
144 if (flash_read(ptn, offset, buf, pagesize)) {
145 dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n");
146 return -1;
147 }
148 memcpy(header, buf, sizeof(*header));
149
Greg Griscod6250552011-06-29 14:40:23 -0700150 if (strncmp((char *) header->MAGIC, UPDATE_MAGIC, UPDATE_MAGIC_SIZE))
Shashank Mittal024c0332010-02-03 11:44:00 -0800151 {
152 return -1;
153 }
154 return 0;
155}
156
157int update_firmware_image (struct update_header *header, char *name)
158{
159 struct ptentry *ptn;
160 struct ptable *ptable;
161 unsigned offset = 0;
162 unsigned pagesize = flash_page_size();
163 unsigned pagemask = pagesize -1;
164 unsigned n = 0;
165
166 ptable = flash_get_ptable();
167 if (ptable == NULL) {
168 dprintf(CRITICAL, "ERROR: Partition table not found\n");
169 return -1;
170 }
171
172 ptn = ptable_find(ptable, "cache");
173 if (ptn == NULL) {
174 dprintf(CRITICAL, "ERROR: No cache partition found\n");
175 return -1;
176 }
177
178 offset += header->image_offset;
179 n = (header->image_length + pagemask) & (~pagemask);
180
Greg Griscod6250552011-06-29 14:40:23 -0700181 if (flash_read(ptn, offset, (void *) SCRATCH_ADDR, n)) {
Shashank Mittal024c0332010-02-03 11:44:00 -0800182 dprintf(CRITICAL, "ERROR: Cannot read radio image\n");
183 return -1;
184 }
185
186 ptn = ptable_find(ptable, name);
187 if (ptn == NULL) {
188 dprintf(CRITICAL, "ERROR: No %s partition found\n", name);
189 return -1;
190 }
191
Greg Griscod6250552011-06-29 14:40:23 -0700192 if (flash_write(ptn, 0, (void *) SCRATCH_ADDR, n)) {
Shashank Mittal024c0332010-02-03 11:44:00 -0800193 dprintf(CRITICAL, "ERROR: flash write fail!\n");
194 return -1;
195 }
196
197 dprintf(INFO, "Partition writen successfully!");
198 return 0;
199}
200
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700201static int set_ssd_radio_update (char *name)
202{
203 struct ptentry *ptn;
204 struct ptable *ptable;
205 unsigned int ssd_cookie[2] = {0x53534443, 0x4F4F4B49};
206 unsigned pagesize = flash_page_size();
207 unsigned pagemask = pagesize -1;
208 unsigned n = 0;
209
210 ptable = flash_get_ptable();
211 if (ptable == NULL) {
212 dprintf(CRITICAL, "ERROR: Partition table not found\n");
213 return -1;
214 }
215
216 n = (sizeof(ssd_cookie) + pagemask) & (~pagemask);
217
218 ptn = ptable_find(ptable, name);
219 if (ptn == NULL) {
220 dprintf(CRITICAL, "ERROR: No %s partition found\n", name);
221 return -1;
222 }
223
224 if (flash_write(ptn, 0, ssd_cookie, n)) {
225 dprintf(CRITICAL, "ERROR: flash write fail!\n");
226 return -1;
227 }
228
229 dprintf(INFO, "FOTA partition written successfully!");
230 return 0;
231}
232
233int get_boot_info_apps (char type, unsigned int *status)
234{
235 boot_info_for_apps apps_boot_info;
236 int ret = 0;
237
238 ret = smem_read_alloc_entry(SMEM_BOOT_INFO_FOR_APPS,
239 &apps_boot_info, sizeof(apps_boot_info));
240 if (ret)
241 {
242 dprintf(CRITICAL, "ERROR: unable to read shared memory for apps boot info %d\n",ret);
243 return ret;
244 }
245
246 dprintf(INFO,"boot flag %x update status %x\n",apps_boot_info.boot_flags,
247 apps_boot_info.status.update_status);
248
249 if(type == BOOT_FLAGS)
250 *status = apps_boot_info.boot_flags;
251 else if(type == UPDATE_STATUS)
252 *status = apps_boot_info.status.update_status;
253
254 return ret;
255}
256
Shashank Mittal024c0332010-02-03 11:44:00 -0800257/* Bootloader / Recovery Flow
258 *
259 * On every boot, the bootloader will read the recovery_message
260 * from flash and check the command field. The bootloader should
261 * deal with the command field not having a 0 terminator correctly
262 * (so as to not crash if the block is invalid or corrupt).
263 *
264 * The bootloader will have to publish the partition that contains
265 * the recovery_message to the linux kernel so it can update it.
266 *
267 * if command == "boot-recovery" -> boot recovery.img
268 * else if command == "update-radio" -> update radio image (below)
269 * else -> boot boot.img (normal boot)
270 *
271 * Radio Update Flow
272 * 1. the bootloader will attempt to load and validate the header
273 * 2. if the header is invalid, status="invalid-update", goto #8
274 * 3. display the busy image on-screen
275 * 4. if the update image is invalid, status="invalid-radio-image", goto #8
276 * 5. attempt to update the firmware (depending on the command)
277 * 6. if successful, status="okay", goto #8
278 * 7. if failed, and the old image can still boot, status="failed-update"
279 * 8. write the recovery_message, leaving the recovery field
280 * unchanged, updating status, and setting command to
281 * "boot-recovery"
282 * 9. reboot
283 *
284 * The bootloader will not modify or erase the cache partition.
285 * It is recovery's responsibility to clean up the mess afterwards.
286 */
287
288int recovery_init (void)
289{
290 struct recovery_message msg;
Shashank Mittal024c0332010-02-03 11:44:00 -0800291 char partition_name[32];
292 unsigned valid_command = 0;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700293 int update_status = 0;
Shashank Mittal024c0332010-02-03 11:44:00 -0800294
295 // get recovery message
Greg Griscod6250552011-06-29 14:40:23 -0700296 if (get_recovery_message(&msg))
Shashank Mittal024c0332010-02-03 11:44:00 -0800297 return -1;
298 if (msg.command[0] != 0 && msg.command[0] != 255) {
Greg Griscod6250552011-06-29 14:40:23 -0700299 dprintf(INFO, "Recovery command: %.*s\n", sizeof(msg.command), msg.command);
Shashank Mittal024c0332010-02-03 11:44:00 -0800300 }
301 msg.command[sizeof(msg.command)-1] = '\0'; //Ensure termination
302
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700303 if (!strcmp("boot-recovery",msg.command))
304 {
305 if(!strcmp("RADIO",msg.status))
306 {
307 /* We're now here due to radio update, so check for update status */
Greg Griscod2471ef2011-07-14 13:00:42 -0700308 int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700309
310 if(!ret && (update_status & 0x01))
311 {
312 dprintf(INFO,"radio update success\n");
313 strcpy(msg.status, "OKAY");
314 }
315 else
316 {
317 dprintf(INFO,"radio update failed\n");
318 strcpy(msg.status, "failed-update");
319 }
320 strcpy(msg.command, ""); // clearing recovery command
321 set_recovery_message(&msg); // send recovery message
322 boot_into_recovery = 1; // Boot in recovery mode
323 return 0;
324 }
325
Shashank Mittal024c0332010-02-03 11:44:00 -0800326 valid_command = 1;
327 strcpy(msg.command, ""); // to safe against multiple reboot into recovery
328 strcpy(msg.status, "OKAY");
329 set_recovery_message(&msg); // send recovery message
330 boot_into_recovery = 1; // Boot in recovery mode
331 return 0;
332 }
333
334 if (!strcmp("update-radio",msg.command)) {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700335 dprintf(INFO,"start radio update\n");
Shashank Mittal024c0332010-02-03 11:44:00 -0800336 valid_command = 1;
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700337 strcpy(partition_name, "FOTA");
Shashank Mittal024c0332010-02-03 11:44:00 -0800338 }
339
340 //Todo: Add support for bootloader update too.
341
342 if(!valid_command) {
343 //We need not to do anything
344 return 0; // Boot in normal mode
345 }
346
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700347#ifdef OLD_FOTA_UPGRADE
Shashank Mittal024c0332010-02-03 11:44:00 -0800348 if (read_update_header_for_bootloader(&header)) {
349 strcpy(msg.status, "invalid-update");
350 goto SEND_RECOVERY_MSG;
351 }
352
353 if (update_firmware_image (&header, partition_name)) {
354 strcpy(msg.status, "failed-update");
355 goto SEND_RECOVERY_MSG;
356 }
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700357#else
358 if (set_ssd_radio_update(partition_name)) {
359 /* If writing to FOTA partition fails */
360 strcpy(msg.command, "");
361 strcpy(msg.status, "failed-update");
362 goto SEND_RECOVERY_MSG;
363 }
364 else {
365 /* Setting this to check the radio update status */
366 strcpy(msg.command, "boot-recovery");
367 strcpy(msg.status, "RADIO");
368 goto SEND_RECOVERY_MSG;
369 }
370#endif
Shashank Mittal024c0332010-02-03 11:44:00 -0800371 strcpy(msg.status, "OKAY");
372
373SEND_RECOVERY_MSG:
Shashank Mittal024c0332010-02-03 11:44:00 -0800374 set_recovery_message(&msg); // send recovery message
375 boot_into_recovery = 1; // Boot in recovery mode
376 reboot_device(0);
377 return 0;
378}
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700379
380static int emmc_set_recovery_msg(struct recovery_message *out)
381{
382 char *ptn_name = "misc";
383 unsigned long long ptn = 0;
384 unsigned int size = ROUND_TO_PAGE(sizeof(*out),511);
385 unsigned char data[size];
Kinson Chikf1a43512011-07-14 11:28:39 -0700386 int index = INVALID_PTN;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700387
Kinson Chikf1a43512011-07-14 11:28:39 -0700388 index = partition_get_index((unsigned char *) ptn_name);
389 ptn = partition_get_offset(index);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700390 if(ptn == 0) {
391 dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name);
392 return -1;
393 }
394 memcpy(data, out, sizeof(*out));
395 if (mmc_write(ptn , size, (unsigned int*)data)) {
396 dprintf(CRITICAL,"mmc write failure %s %d\n",ptn_name, sizeof(*out));
397 return -1;
398 }
399 return 0;
400}
401
402static int emmc_get_recovery_msg(struct recovery_message *in)
403{
404 char *ptn_name = "misc";
405 unsigned long long ptn = 0;
406 unsigned int size = ROUND_TO_PAGE(sizeof(*in),511);
407 unsigned char data[size];
Kinson Chikf1a43512011-07-14 11:28:39 -0700408 int index = INVALID_PTN;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700409
Kinson Chikf1a43512011-07-14 11:28:39 -0700410 index = partition_get_index((unsigned char *) ptn_name);
411 ptn = partition_get_offset(index);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700412 if(ptn == 0) {
413 dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name);
414 return -1;
415 }
416 if (mmc_read(ptn , (unsigned int*)data, size)) {
417 dprintf(CRITICAL,"mmc read failure %s %d\n",ptn_name, size);
418 return -1;
419 }
420 memcpy(in, data, sizeof(*in));
421 return 0;
422}
423
424int _emmc_recovery_init(void)
425{
426 int update_status = 0;
427 struct recovery_message msg;
428
429 // get recovery message
430 if(emmc_get_recovery_msg(&msg))
431 return -1;
432 if (msg.command[0] != 0 && msg.command[0] != 255) {
433 dprintf(INFO,"Recovery command: %d %s\n", sizeof(msg.command), msg.command);
434 }
435 msg.command[sizeof(msg.command)-1] = '\0'; //Ensure termination
436
437 if (!strcmp("update-radio",msg.command))
438 {
439 /* We're now here due to radio update, so check for update status */
Greg Griscod2471ef2011-07-14 13:00:42 -0700440 int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700441
442 if(!ret && (update_status & 0x01))
443 {
444 dprintf(INFO,"radio update success\n");
445 strcpy(msg.status, "OKAY");
446 }
447 else
448 {
449 dprintf(INFO,"radio update failed\n");
450 strcpy(msg.status, "failed-update");
451 }
Shashank Mittal162244e2011-08-08 19:01:25 -0700452 boot_into_recovery = 1; // Boot in recovery mode
453 }
454 if (!strcmp("reset-device-info",msg.command))
455 {
456 reset_device_info();
457 }
458 if (!strcmp("root-detect",msg.command))
459 {
460 set_device_root();
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700461 }
462 else
463 return 0; // do nothing
464
465 strcpy(msg.command, ""); // clearing recovery command
466 emmc_set_recovery_msg(&msg); // send recovery message
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700467 return 0;
468}