Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved. |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 2 | |
| 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 Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 39 | #include <platform.h> |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 40 | #include <partition_parser.h> |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 41 | #include <mmc.h> |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 42 | |
| 43 | #include "recovery.h" |
| 44 | #include "bootimg.h" |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 45 | #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 Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 50 | |
| 51 | static const int MISC_PAGES = 3; // number of pages to save |
| 52 | static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page |
| 53 | static char buf[4096]; |
| 54 | unsigned boot_into_recovery = 0; |
| 55 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 56 | extern void reset_device_info(); |
| 57 | extern void set_device_root(); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 58 | |
| 59 | int 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 Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 80 | if (flash_read(ptn, offset, (void *) buf, pagesize)) { |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 81 | dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n"); |
| 82 | return -1; |
| 83 | } |
| 84 | memcpy(out, buf, sizeof(*out)); |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | int 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 Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 111 | if (flash_read(ptn, offset, (void *) SCRATCH_ADDR, n)) { |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 112 | dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n"); |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | offset += (pagesize * MISC_COMMAND_PAGE); |
| 117 | offset += SCRATCH_ADDR; |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 118 | memcpy((void *) offset, in, sizeof(*in)); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 119 | if (flash_write(ptn, 0, (void *)SCRATCH_ADDR, n)) { |
| 120 | dprintf(CRITICAL, "ERROR: flash write fail!\n"); |
| 121 | return -1; |
| 122 | } |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 123 | return 0; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | int 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 Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 150 | if (strncmp((char *) header->MAGIC, UPDATE_MAGIC, UPDATE_MAGIC_SIZE)) |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 151 | { |
| 152 | return -1; |
| 153 | } |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | int 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 Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 181 | if (flash_read(ptn, offset, (void *) SCRATCH_ADDR, n)) { |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 182 | 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 Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 192 | if (flash_write(ptn, 0, (void *) SCRATCH_ADDR, n)) { |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 193 | dprintf(CRITICAL, "ERROR: flash write fail!\n"); |
| 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | dprintf(INFO, "Partition writen successfully!"); |
| 198 | return 0; |
| 199 | } |
| 200 | |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 201 | static 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 | |
| 233 | int 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 Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 257 | /* 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 | |
| 288 | int recovery_init (void) |
| 289 | { |
| 290 | struct recovery_message msg; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 291 | char partition_name[32]; |
| 292 | unsigned valid_command = 0; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 293 | int update_status = 0; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 294 | |
| 295 | // get recovery message |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 296 | if (get_recovery_message(&msg)) |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 297 | return -1; |
| 298 | if (msg.command[0] != 0 && msg.command[0] != 255) { |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 299 | dprintf(INFO, "Recovery command: %.*s\n", sizeof(msg.command), msg.command); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 300 | } |
| 301 | msg.command[sizeof(msg.command)-1] = '\0'; //Ensure termination |
| 302 | |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 303 | 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 Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 308 | int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 309 | |
| 310 | if(!ret && (update_status & 0x01)) |
| 311 | { |
| 312 | dprintf(INFO,"radio update success\n"); |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 313 | strlcpy(msg.status, "OKAY", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 314 | } |
| 315 | else |
| 316 | { |
| 317 | dprintf(INFO,"radio update failed\n"); |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 318 | strlcpy(msg.status, "failed-update", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 319 | } |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 320 | strlcpy(msg.command, "", sizeof(msg.command)); // clearing recovery command |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 321 | set_recovery_message(&msg); // send recovery message |
| 322 | boot_into_recovery = 1; // Boot in recovery mode |
| 323 | return 0; |
| 324 | } |
| 325 | |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 326 | valid_command = 1; |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 327 | strlcpy(msg.command, "", sizeof(msg.command)); // to safe against multiple reboot into recovery |
| 328 | strlcpy(msg.status, "OKAY", sizeof(msg.status)); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 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 Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 335 | dprintf(INFO,"start radio update\n"); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 336 | valid_command = 1; |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 337 | strlcpy(partition_name, "FOTA", sizeof(partition_name)); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 338 | } |
| 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 Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 347 | #ifdef OLD_FOTA_UPGRADE |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 348 | if (read_update_header_for_bootloader(&header)) { |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 349 | strlcpy(msg.status, "invalid-update", sizeof(msg.status)); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 350 | goto SEND_RECOVERY_MSG; |
| 351 | } |
| 352 | |
| 353 | if (update_firmware_image (&header, partition_name)) { |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 354 | strlcpy(msg.status, "failed-update", sizeof(msg.status)); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 355 | goto SEND_RECOVERY_MSG; |
| 356 | } |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 357 | #else |
| 358 | if (set_ssd_radio_update(partition_name)) { |
| 359 | /* If writing to FOTA partition fails */ |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 360 | strlcpy(msg.command, "", sizeof(msg.command)); |
| 361 | strlcpy(msg.status, "failed-update", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 362 | goto SEND_RECOVERY_MSG; |
| 363 | } |
| 364 | else { |
| 365 | /* Setting this to check the radio update status */ |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 366 | strlcpy(msg.command, "boot-recovery", sizeof(msg.command)); |
Ajay Dudani | f4caa94 | 2011-10-02 08:57:15 -0700 | [diff] [blame] | 367 | strlcpy(msg.status, "RADIO", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 368 | goto SEND_RECOVERY_MSG; |
| 369 | } |
| 370 | #endif |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 371 | strlcpy(msg.status, "OKAY", sizeof(msg.status)); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 372 | |
| 373 | SEND_RECOVERY_MSG: |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 374 | 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 Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 379 | |
| 380 | static 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 Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 386 | int index = INVALID_PTN; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 387 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 388 | index = partition_get_index((unsigned char *) ptn_name); |
| 389 | ptn = partition_get_offset(index); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 390 | 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 | |
| 402 | static 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 Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 408 | int index = INVALID_PTN; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 409 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 410 | index = partition_get_index((unsigned char *) ptn_name); |
| 411 | ptn = partition_get_offset(index); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 412 | 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 | |
| 424 | int _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 Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 440 | int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 441 | |
| 442 | if(!ret && (update_status & 0x01)) |
| 443 | { |
| 444 | dprintf(INFO,"radio update success\n"); |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 445 | strlcpy(msg.status, "OKAY", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 446 | } |
| 447 | else |
| 448 | { |
| 449 | dprintf(INFO,"radio update failed\n"); |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 450 | strlcpy(msg.status, "failed-update", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 451 | } |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 452 | 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 Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 461 | } |
| 462 | else |
| 463 | return 0; // do nothing |
| 464 | |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 465 | strlcpy(msg.command, "", sizeof(msg.command)); // clearing recovery command |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 466 | emmc_set_recovery_msg(&msg); // send recovery message |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 467 | return 0; |
| 468 | } |