Kishor PK | 8a2783f | 2017-04-20 14:51:04 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2010-2017, The Linux Foundation. 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. |
Duy Truong | f3ac7b3 | 2013-02-13 01:07:28 -0800 | [diff] [blame] | 12 | * * Neither the name of The Linux Foundation nor the names of its |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 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> |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 35 | #include <arch/defines.h> |
| 36 | #include <malloc.h> |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 37 | |
| 38 | #include <dev/flash.h> |
| 39 | #include <lib/ptable.h> |
| 40 | #include <dev/keys.h> |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 41 | #include <platform.h> |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 42 | #include <target.h> |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 43 | #include <partition_parser.h> |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 44 | #include <mmc.h> |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 45 | #include <malloc.h> |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 46 | |
| 47 | #include "recovery.h" |
| 48 | #include "bootimg.h" |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 49 | #include "smem.h" |
| 50 | |
| 51 | #define BOOT_FLAGS 1 |
| 52 | #define UPDATE_STATUS 2 |
| 53 | #define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y))) |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 54 | |
| 55 | static const int MISC_PAGES = 3; // number of pages to save |
| 56 | static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page |
| 57 | static char buf[4096]; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 58 | |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 59 | unsigned boot_into_recovery = 0; |
| 60 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 61 | extern uint32_t get_page_size(); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 62 | extern void reset_device_info(); |
| 63 | extern void set_device_root(); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 64 | |
| 65 | int get_recovery_message(struct recovery_message *out) |
| 66 | { |
| 67 | struct ptentry *ptn; |
| 68 | struct ptable *ptable; |
| 69 | unsigned offset = 0; |
| 70 | unsigned pagesize = flash_page_size(); |
| 71 | |
| 72 | ptable = flash_get_ptable(); |
| 73 | |
| 74 | if (ptable == NULL) { |
| 75 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 76 | return -1; |
| 77 | } |
| 78 | ptn = ptable_find(ptable, "misc"); |
| 79 | |
| 80 | if (ptn == NULL) { |
| 81 | dprintf(CRITICAL, "ERROR: No misc partition found\n"); |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | offset += (pagesize * MISC_COMMAND_PAGE); |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 86 | if (flash_read(ptn, offset, (void *) buf, pagesize)) { |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 87 | dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n"); |
| 88 | return -1; |
| 89 | } |
| 90 | memcpy(out, buf, sizeof(*out)); |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int set_recovery_message(const struct recovery_message *in) |
| 95 | { |
| 96 | struct ptentry *ptn; |
| 97 | struct ptable *ptable; |
| 98 | unsigned offset = 0; |
| 99 | unsigned pagesize = flash_page_size(); |
| 100 | unsigned n = 0; |
Pavel Nedev | 5d4a705 | 2013-05-13 15:14:45 +0300 | [diff] [blame] | 101 | void *scratch_addr = target_get_scratch_address(); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 102 | |
| 103 | ptable = flash_get_ptable(); |
| 104 | |
| 105 | if (ptable == NULL) { |
| 106 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 107 | return -1; |
| 108 | } |
| 109 | ptn = ptable_find(ptable, "misc"); |
| 110 | |
| 111 | if (ptn == NULL) { |
| 112 | dprintf(CRITICAL, "ERROR: No misc partition found\n"); |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | n = pagesize * (MISC_COMMAND_PAGE + 1); |
| 117 | |
Pavel Nedev | 5d4a705 | 2013-05-13 15:14:45 +0300 | [diff] [blame] | 118 | if (flash_read(ptn, offset, scratch_addr, n)) { |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 119 | dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n"); |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | offset += (pagesize * MISC_COMMAND_PAGE); |
Pavel Nedev | 5d4a705 | 2013-05-13 15:14:45 +0300 | [diff] [blame] | 124 | offset += (unsigned) scratch_addr; |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 125 | memcpy((void *) offset, in, sizeof(*in)); |
Pavel Nedev | 5d4a705 | 2013-05-13 15:14:45 +0300 | [diff] [blame] | 126 | if (flash_write(ptn, 0, scratch_addr, n)) { |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 127 | dprintf(CRITICAL, "ERROR: flash write fail!\n"); |
| 128 | return -1; |
| 129 | } |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 130 | return 0; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 133 | static int set_ssd_radio_update (char *name) |
| 134 | { |
| 135 | struct ptentry *ptn; |
| 136 | struct ptable *ptable; |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 137 | unsigned int *ssd_cookie; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 138 | unsigned pagesize = flash_page_size(); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 139 | |
| 140 | ptable = flash_get_ptable(); |
| 141 | if (ptable == NULL) { |
| 142 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 143 | return -1; |
| 144 | } |
| 145 | |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 146 | ssd_cookie = malloc(pagesize); |
| 147 | if (!ssd_cookie){ |
| 148 | dprintf(CRITICAL, "ERROR: Memory allocation failure\n"); |
| 149 | return -1; |
| 150 | } |
| 151 | memset(ssd_cookie, 0, pagesize); |
| 152 | ssd_cookie[0] = 0x53534443; |
| 153 | ssd_cookie[1] = 0x4F4F4B49; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 154 | |
| 155 | ptn = ptable_find(ptable, name); |
| 156 | if (ptn == NULL) { |
| 157 | dprintf(CRITICAL, "ERROR: No %s partition found\n", name); |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 158 | goto out; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 161 | if (flash_write(ptn, 0, ssd_cookie, pagesize)) { |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 162 | dprintf(CRITICAL, "ERROR: flash write fail!\n"); |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 163 | goto out; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 166 | free(ssd_cookie); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 167 | dprintf(INFO, "FOTA partition written successfully!"); |
| 168 | return 0; |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 169 | out: |
| 170 | free(ssd_cookie); |
| 171 | return -1; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | int get_boot_info_apps (char type, unsigned int *status) |
| 175 | { |
| 176 | boot_info_for_apps apps_boot_info; |
| 177 | int ret = 0; |
| 178 | |
| 179 | ret = smem_read_alloc_entry(SMEM_BOOT_INFO_FOR_APPS, |
| 180 | &apps_boot_info, sizeof(apps_boot_info)); |
| 181 | if (ret) |
| 182 | { |
| 183 | dprintf(CRITICAL, "ERROR: unable to read shared memory for apps boot info %d\n",ret); |
| 184 | return ret; |
| 185 | } |
| 186 | |
| 187 | dprintf(INFO,"boot flag %x update status %x\n",apps_boot_info.boot_flags, |
| 188 | apps_boot_info.status.update_status); |
| 189 | |
| 190 | if(type == BOOT_FLAGS) |
| 191 | *status = apps_boot_info.boot_flags; |
| 192 | else if(type == UPDATE_STATUS) |
| 193 | *status = apps_boot_info.status.update_status; |
| 194 | |
| 195 | return ret; |
| 196 | } |
| 197 | |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 198 | /* Bootloader / Recovery Flow |
| 199 | * |
| 200 | * On every boot, the bootloader will read the recovery_message |
| 201 | * from flash and check the command field. The bootloader should |
| 202 | * deal with the command field not having a 0 terminator correctly |
| 203 | * (so as to not crash if the block is invalid or corrupt). |
| 204 | * |
| 205 | * The bootloader will have to publish the partition that contains |
| 206 | * the recovery_message to the linux kernel so it can update it. |
| 207 | * |
| 208 | * if command == "boot-recovery" -> boot recovery.img |
| 209 | * else if command == "update-radio" -> update radio image (below) |
| 210 | * else -> boot boot.img (normal boot) |
| 211 | * |
| 212 | * Radio Update Flow |
| 213 | * 1. the bootloader will attempt to load and validate the header |
| 214 | * 2. if the header is invalid, status="invalid-update", goto #8 |
| 215 | * 3. display the busy image on-screen |
| 216 | * 4. if the update image is invalid, status="invalid-radio-image", goto #8 |
| 217 | * 5. attempt to update the firmware (depending on the command) |
| 218 | * 6. if successful, status="okay", goto #8 |
| 219 | * 7. if failed, and the old image can still boot, status="failed-update" |
| 220 | * 8. write the recovery_message, leaving the recovery field |
| 221 | * unchanged, updating status, and setting command to |
| 222 | * "boot-recovery" |
| 223 | * 9. reboot |
| 224 | * |
| 225 | * The bootloader will not modify or erase the cache partition. |
| 226 | * It is recovery's responsibility to clean up the mess afterwards. |
| 227 | */ |
| 228 | |
| 229 | int recovery_init (void) |
| 230 | { |
| 231 | struct recovery_message msg; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 232 | char partition_name[32]; |
| 233 | unsigned valid_command = 0; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 234 | int update_status = 0; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 235 | |
| 236 | // get recovery message |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 237 | if (get_recovery_message(&msg)) |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 238 | return -1; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 239 | msg.command[sizeof(msg.command)-1] = '\0'; //Ensure termination |
Pavel Nedev | 96a9aea | 2013-02-26 15:16:26 -0800 | [diff] [blame] | 240 | if (msg.command[0] != 0 && msg.command[0] != 255) { |
| 241 | dprintf(INFO,"Recovery command: %d %s\n", |
| 242 | sizeof(msg.command), msg.command); |
| 243 | } |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 244 | |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 245 | if (!strcmp("boot-recovery",msg.command)) |
| 246 | { |
| 247 | if(!strcmp("RADIO",msg.status)) |
| 248 | { |
| 249 | /* 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] | 250 | int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 251 | |
| 252 | if(!ret && (update_status & 0x01)) |
| 253 | { |
| 254 | dprintf(INFO,"radio update success\n"); |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 255 | strlcpy(msg.status, "OKAY", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 256 | } |
| 257 | else |
| 258 | { |
| 259 | dprintf(INFO,"radio update failed\n"); |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 260 | strlcpy(msg.status, "failed-update", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 261 | } |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 262 | strlcpy(msg.command, "", sizeof(msg.command)); // clearing recovery command |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 263 | set_recovery_message(&msg); // send recovery message |
| 264 | boot_into_recovery = 1; // Boot in recovery mode |
| 265 | return 0; |
| 266 | } |
| 267 | |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 268 | valid_command = 1; |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 269 | strlcpy(msg.command, "", sizeof(msg.command)); // to safe against multiple reboot into recovery |
| 270 | strlcpy(msg.status, "OKAY", sizeof(msg.status)); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 271 | set_recovery_message(&msg); // send recovery message |
| 272 | boot_into_recovery = 1; // Boot in recovery mode |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | if (!strcmp("update-radio",msg.command)) { |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 277 | dprintf(INFO,"start radio update\n"); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 278 | valid_command = 1; |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 279 | strlcpy(partition_name, "FOTA", sizeof(partition_name)); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | //Todo: Add support for bootloader update too. |
| 283 | |
| 284 | if(!valid_command) { |
| 285 | //We need not to do anything |
| 286 | return 0; // Boot in normal mode |
| 287 | } |
| 288 | |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 289 | if (set_ssd_radio_update(partition_name)) { |
| 290 | /* If writing to FOTA partition fails */ |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 291 | strlcpy(msg.command, "", sizeof(msg.command)); |
| 292 | strlcpy(msg.status, "failed-update", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 293 | goto SEND_RECOVERY_MSG; |
| 294 | } |
| 295 | else { |
| 296 | /* Setting this to check the radio update status */ |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 297 | strlcpy(msg.command, "boot-recovery", sizeof(msg.command)); |
Ajay Dudani | f4caa94 | 2011-10-02 08:57:15 -0700 | [diff] [blame] | 298 | strlcpy(msg.status, "RADIO", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 299 | goto SEND_RECOVERY_MSG; |
| 300 | } |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 301 | strlcpy(msg.status, "OKAY", sizeof(msg.status)); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 302 | |
| 303 | SEND_RECOVERY_MSG: |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 304 | set_recovery_message(&msg); // send recovery message |
| 305 | boot_into_recovery = 1; // Boot in recovery mode |
| 306 | reboot_device(0); |
| 307 | return 0; |
| 308 | } |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 309 | |
| 310 | static int emmc_set_recovery_msg(struct recovery_message *out) |
| 311 | { |
| 312 | char *ptn_name = "misc"; |
| 313 | unsigned long long ptn = 0; |
| 314 | unsigned int size = ROUND_TO_PAGE(sizeof(*out),511); |
| 315 | unsigned char data[size]; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 316 | int index = INVALID_PTN; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 317 | |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 318 | index = partition_get_index((const char *) ptn_name); |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 319 | ptn = partition_get_offset(index); |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 320 | mmc_set_lun(partition_get_lun(index)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 321 | if(ptn == 0) { |
| 322 | dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name); |
| 323 | return -1; |
| 324 | } |
| 325 | memcpy(data, out, sizeof(*out)); |
| 326 | if (mmc_write(ptn , size, (unsigned int*)data)) { |
| 327 | dprintf(CRITICAL,"mmc write failure %s %d\n",ptn_name, sizeof(*out)); |
| 328 | return -1; |
| 329 | } |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static int emmc_get_recovery_msg(struct recovery_message *in) |
| 334 | { |
| 335 | char *ptn_name = "misc"; |
| 336 | unsigned long long ptn = 0; |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 337 | unsigned int size; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 338 | int index = INVALID_PTN; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 339 | |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 340 | size = mmc_get_device_blocksize(); |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 341 | index = partition_get_index((const char *) ptn_name); |
Channagoud Kadabi | f280563 | 2015-01-07 16:46:50 -0800 | [diff] [blame] | 342 | if (index < 0) |
| 343 | { |
| 344 | dprintf(CRITICAL, "%s: Partition not found\n", ptn_name); |
| 345 | return -1; |
| 346 | } |
| 347 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 348 | ptn = partition_get_offset(index); |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 349 | mmc_set_lun(partition_get_lun(index)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 350 | if(ptn == 0) { |
| 351 | dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name); |
| 352 | return -1; |
| 353 | } |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 354 | if (mmc_read(ptn , (unsigned int*)in, size)) { |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 355 | dprintf(CRITICAL,"mmc read failure %s %d\n",ptn_name, size); |
| 356 | return -1; |
| 357 | } |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 358 | |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | int _emmc_recovery_init(void) |
| 363 | { |
| 364 | int update_status = 0; |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 365 | struct recovery_message *msg; |
| 366 | uint32_t block_size = 0; |
| 367 | |
| 368 | block_size = mmc_get_device_blocksize(); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 369 | |
| 370 | // get recovery message |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 371 | msg = (struct recovery_message *)memalign(CACHE_LINE, block_size); |
| 372 | ASSERT(msg); |
| 373 | |
| 374 | if(emmc_get_recovery_msg(msg)) |
| 375 | { |
| 376 | if(msg) |
| 377 | free(msg); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 378 | return -1; |
Pavel Nedev | 96a9aea | 2013-02-26 15:16:26 -0800 | [diff] [blame] | 379 | } |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 380 | |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 381 | msg->command[sizeof(msg->command)-1] = '\0'; //Ensure termination |
| 382 | if (msg->command[0] != 0 && msg->command[0] != 255) { |
| 383 | dprintf(INFO,"Recovery command: %d %s\n", |
| 384 | sizeof(msg->command), msg->command); |
| 385 | } |
| 386 | |
vijay kumar | 32e2e3c | 2014-08-01 21:10:28 +0530 | [diff] [blame] | 387 | if (!strcmp(msg->command, "boot-recovery")) { |
Stanimir Varbanov | be04199 | 2013-04-26 14:29:21 +0300 | [diff] [blame] | 388 | boot_into_recovery = 1; |
| 389 | } |
| 390 | |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 391 | if (!strcmp("update-radio",msg->command)) |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 392 | { |
| 393 | /* 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] | 394 | int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 395 | |
| 396 | if(!ret && (update_status & 0x01)) |
| 397 | { |
| 398 | dprintf(INFO,"radio update success\n"); |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 399 | strlcpy(msg->status, "OKAY", sizeof(msg->status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 400 | } |
| 401 | else |
| 402 | { |
| 403 | dprintf(INFO,"radio update failed\n"); |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 404 | strlcpy(msg->status, "failed-update", sizeof(msg->status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 405 | } |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 406 | boot_into_recovery = 1; // Boot in recovery mode |
| 407 | } |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 408 | if (!strcmp("reset-device-info",msg->command)) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 409 | { |
| 410 | reset_device_info(); |
| 411 | } |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 412 | if (!strcmp("root-detect",msg->command)) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 413 | { |
| 414 | set_device_root(); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 415 | } |
| 416 | else |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 417 | goto out;// do nothing |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 418 | |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 419 | strlcpy(msg->command, "", sizeof(msg->command)); // clearing recovery command |
| 420 | emmc_set_recovery_msg(msg); // send recovery message |
| 421 | |
| 422 | out: |
| 423 | if(msg) |
| 424 | free(msg); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 425 | return 0; |
| 426 | } |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 427 | |
| 428 | static int read_misc(unsigned page_offset, void *buf, unsigned size) |
| 429 | { |
| 430 | const char *ptn_name = "misc"; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 431 | uint32_t pagesize = get_page_size(); |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 432 | unsigned offset; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 433 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 434 | if (size == 0 || buf == NULL) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 435 | return -1; |
| 436 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 437 | offset = page_offset * pagesize; |
| 438 | |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 439 | if (target_is_emmc_boot()) |
| 440 | { |
| 441 | int index; |
| 442 | unsigned long long ptn; |
| 443 | unsigned long long ptn_size; |
| 444 | |
| 445 | index = partition_get_index(ptn_name); |
| 446 | if (index == INVALID_PTN) |
| 447 | { |
| 448 | dprintf(CRITICAL, "No '%s' partition found\n", ptn_name); |
| 449 | return -1; |
| 450 | } |
| 451 | |
| 452 | ptn = partition_get_offset(index); |
| 453 | ptn_size = partition_get_size(index); |
| 454 | |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 455 | mmc_set_lun(partition_get_lun(index)); |
| 456 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 457 | if (ptn_size < offset + size) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 458 | { |
| 459 | dprintf(CRITICAL, "Read request out of '%s' boundaries\n", |
| 460 | ptn_name); |
| 461 | return -1; |
| 462 | } |
| 463 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 464 | if (mmc_read(ptn + offset, (unsigned int *)buf, size)) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 465 | { |
| 466 | dprintf(CRITICAL, "Reading MMC failed\n"); |
| 467 | return -1; |
| 468 | } |
| 469 | } |
| 470 | else |
| 471 | { |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 472 | dprintf(CRITICAL, "Misc partition not supported for NAND targets.\n"); |
| 473 | return -1; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 474 | } |
| 475 | |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 476 | return 0; |
| 477 | } |
| 478 | |
Pavel Nedev | c728bb3 | 2013-04-05 16:58:05 +0300 | [diff] [blame] | 479 | int write_misc(unsigned page_offset, void *buf, unsigned size) |
| 480 | { |
| 481 | const char *ptn_name = "misc"; |
| 482 | void *scratch_addr = target_get_scratch_address(); |
| 483 | unsigned offset; |
| 484 | unsigned aligned_size; |
| 485 | |
| 486 | if (size == 0 || buf == NULL || scratch_addr == NULL) |
| 487 | return -1; |
| 488 | |
| 489 | if (target_is_emmc_boot()) |
| 490 | { |
| 491 | int index; |
| 492 | unsigned long long ptn; |
| 493 | unsigned long long ptn_size; |
| 494 | |
| 495 | index = partition_get_index(ptn_name); |
| 496 | if (index == INVALID_PTN) |
| 497 | { |
| 498 | dprintf(CRITICAL, "No '%s' partition found\n", ptn_name); |
| 499 | return -1; |
| 500 | } |
| 501 | |
| 502 | ptn = partition_get_offset(index); |
| 503 | ptn_size = partition_get_size(index); |
| 504 | |
| 505 | offset = page_offset * BLOCK_SIZE; |
| 506 | aligned_size = ROUND_TO_PAGE(size, (unsigned)BLOCK_SIZE - 1); |
| 507 | if (ptn_size < offset + aligned_size) |
| 508 | { |
| 509 | dprintf(CRITICAL, "Write request out of '%s' boundaries\n", |
| 510 | ptn_name); |
| 511 | return -1; |
| 512 | } |
| 513 | |
| 514 | if (scratch_addr != buf) |
| 515 | memcpy(scratch_addr, buf, size); |
Channagoud Kadabi | 2776b65 | 2015-08-28 15:41:26 -0700 | [diff] [blame] | 516 | |
| 517 | /* Set Lun for misc partition */ |
| 518 | mmc_set_lun(partition_get_lun(index)); |
| 519 | |
Pavel Nedev | c728bb3 | 2013-04-05 16:58:05 +0300 | [diff] [blame] | 520 | if (mmc_write(ptn + offset, aligned_size, (unsigned int *)scratch_addr)) |
| 521 | { |
| 522 | dprintf(CRITICAL, "Writing MMC failed\n"); |
| 523 | return -1; |
| 524 | } |
| 525 | } |
| 526 | else |
| 527 | { |
| 528 | struct ptentry *ptn; |
| 529 | struct ptable *ptable; |
| 530 | unsigned pagesize = flash_page_size(); |
| 531 | |
| 532 | ptable = flash_get_ptable(); |
| 533 | if (ptable == NULL) |
| 534 | { |
| 535 | dprintf(CRITICAL, "Partition table not found\n"); |
| 536 | return -1; |
| 537 | } |
| 538 | |
| 539 | ptn = ptable_find(ptable, ptn_name); |
| 540 | if (ptn == NULL) |
| 541 | { |
| 542 | dprintf(CRITICAL, "No '%s' partition found\n", ptn_name); |
| 543 | return -1; |
| 544 | } |
| 545 | |
| 546 | offset = page_offset * pagesize; |
| 547 | aligned_size = ROUND_TO_PAGE(size, pagesize - 1); |
| 548 | if (ptn->length < offset + aligned_size) |
| 549 | { |
| 550 | dprintf(CRITICAL, "Write request out of '%s' boundaries\n", |
| 551 | ptn_name); |
| 552 | return -1; |
| 553 | } |
| 554 | |
| 555 | if (scratch_addr != buf) |
| 556 | memcpy(scratch_addr, buf, size); |
| 557 | if (flash_write(ptn, offset, scratch_addr, aligned_size)) { |
| 558 | dprintf(CRITICAL, "Writing flash failed\n"); |
| 559 | return -1; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 566 | int get_ffbm(char *ffbm, unsigned size) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 567 | { |
| 568 | const char *ffbm_cmd = "ffbm-"; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 569 | uint32_t page_size = get_page_size(); |
| 570 | char *ffbm_page_buffer = NULL; |
| 571 | int retval = 0; |
| 572 | if (size < FFBM_MODE_BUF_SIZE || size >= page_size) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 573 | { |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 574 | dprintf(CRITICAL, "Invalid size argument passed to get_ffbm\n"); |
| 575 | retval = -1; |
| 576 | goto cleanup; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 577 | } |
Parth Dixit | 75b1681 | 2016-04-05 19:17:17 +0530 | [diff] [blame] | 578 | ffbm_page_buffer = (char*)memalign(CACHE_LINE, page_size); |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 579 | if (!ffbm_page_buffer) |
| 580 | { |
| 581 | dprintf(CRITICAL, "Failed to alloc buffer for ffbm cookie\n"); |
| 582 | retval = -1; |
| 583 | goto cleanup; |
| 584 | } |
| 585 | if (read_misc(0, ffbm_page_buffer, page_size)) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 586 | { |
| 587 | dprintf(CRITICAL, "Error reading MISC partition\n"); |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 588 | retval = -1; |
| 589 | goto cleanup; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 590 | } |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 591 | ffbm_page_buffer[size] = '\0'; |
| 592 | if (strncmp(ffbm_cmd, ffbm_page_buffer, strlen(ffbm_cmd))) |
| 593 | { |
| 594 | retval = 0; |
| 595 | goto cleanup; |
| 596 | } |
| 597 | else |
| 598 | { |
| 599 | if (strlcpy(ffbm, ffbm_page_buffer, size) < |
| 600 | FFBM_MODE_BUF_SIZE -1) |
| 601 | { |
| 602 | dprintf(CRITICAL, "Invalid string in misc partition\n"); |
| 603 | retval = -1; |
| 604 | } |
| 605 | else |
| 606 | retval = 1; |
| 607 | } |
| 608 | cleanup: |
| 609 | if(ffbm_page_buffer) |
| 610 | free(ffbm_page_buffer); |
| 611 | return retval; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 612 | } |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 613 | |
| 614 | |