blob: cecfb270137db5e70ca388826649c5885e53c507 [file] [log] [blame]
Kishor PK8a2783f2017-04-20 14:51:04 +05301/* Copyright (c) 2010-2017, The Linux Foundation. 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.
Duy Truongf3ac7b32013-02-13 01:07:28 -080012 * * Neither the name of The Linux Foundation nor the names of its
Shashank Mittal024c0332010-02-03 11:44:00 -080013 * 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 Srinivasan151c04d2014-07-17 15:52:12 -070035#include <arch/defines.h>
36#include <malloc.h>
Shashank Mittal024c0332010-02-03 11:44:00 -080037
38#include <dev/flash.h>
39#include <lib/ptable.h>
40#include <dev/keys.h>
Greg Griscod6250552011-06-29 14:40:23 -070041#include <platform.h>
Pavel Nedeva1e62322013-04-05 15:21:36 +030042#include <target.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070043#include <partition_parser.h>
Greg Griscod2471ef2011-07-14 13:00:42 -070044#include <mmc.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053045#include <malloc.h>
Shashank Mittal024c0332010-02-03 11:44:00 -080046
47#include "recovery.h"
48#include "bootimg.h"
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -070049#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 Mittal024c0332010-02-03 11:44:00 -080054
55static const int MISC_PAGES = 3; // number of pages to save
56static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page
57static char buf[4096];
Pavel Nedeva1e62322013-04-05 15:21:36 +030058
Shashank Mittal024c0332010-02-03 11:44:00 -080059unsigned boot_into_recovery = 0;
60
Deepa Dinamani41fa8d62013-05-23 13:25:36 -070061extern uint32_t get_page_size();
Shashank Mittal162244e2011-08-08 19:01:25 -070062extern void reset_device_info();
63extern void set_device_root();
Shashank Mittal024c0332010-02-03 11:44:00 -080064
65int 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 Griscod6250552011-06-29 14:40:23 -070086 if (flash_read(ptn, offset, (void *) buf, pagesize)) {
Shashank Mittal024c0332010-02-03 11:44:00 -080087 dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n");
88 return -1;
89 }
90 memcpy(out, buf, sizeof(*out));
91 return 0;
92}
93
94int 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 Nedev5d4a7052013-05-13 15:14:45 +0300101 void *scratch_addr = target_get_scratch_address();
Shashank Mittal024c0332010-02-03 11:44:00 -0800102
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 Nedev5d4a7052013-05-13 15:14:45 +0300118 if (flash_read(ptn, offset, scratch_addr, n)) {
Shashank Mittal024c0332010-02-03 11:44:00 -0800119 dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n");
120 return -1;
121 }
122
123 offset += (pagesize * MISC_COMMAND_PAGE);
Pavel Nedev5d4a7052013-05-13 15:14:45 +0300124 offset += (unsigned) scratch_addr;
Greg Griscod6250552011-06-29 14:40:23 -0700125 memcpy((void *) offset, in, sizeof(*in));
Pavel Nedev5d4a7052013-05-13 15:14:45 +0300126 if (flash_write(ptn, 0, scratch_addr, n)) {
Shashank Mittal024c0332010-02-03 11:44:00 -0800127 dprintf(CRITICAL, "ERROR: flash write fail!\n");
128 return -1;
129 }
Greg Griscod6250552011-06-29 14:40:23 -0700130 return 0;
Shashank Mittal024c0332010-02-03 11:44:00 -0800131}
132
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700133static int set_ssd_radio_update (char *name)
134{
135 struct ptentry *ptn;
136 struct ptable *ptable;
Mayank Groverba853de2017-06-15 16:20:18 +0530137 unsigned int *ssd_cookie;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700138 unsigned pagesize = flash_page_size();
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700139
140 ptable = flash_get_ptable();
141 if (ptable == NULL) {
142 dprintf(CRITICAL, "ERROR: Partition table not found\n");
143 return -1;
144 }
145
Mayank Groverba853de2017-06-15 16:20:18 +0530146 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 Narayanamurthy0e445b02011-06-19 21:34:46 -0700154
155 ptn = ptable_find(ptable, name);
156 if (ptn == NULL) {
157 dprintf(CRITICAL, "ERROR: No %s partition found\n", name);
Mayank Groverba853de2017-06-15 16:20:18 +0530158 goto out;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700159 }
160
Mayank Groverba853de2017-06-15 16:20:18 +0530161 if (flash_write(ptn, 0, ssd_cookie, pagesize)) {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700162 dprintf(CRITICAL, "ERROR: flash write fail!\n");
Mayank Groverba853de2017-06-15 16:20:18 +0530163 goto out;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700164 }
165
Mayank Groverba853de2017-06-15 16:20:18 +0530166 free(ssd_cookie);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700167 dprintf(INFO, "FOTA partition written successfully!");
168 return 0;
Mayank Groverba853de2017-06-15 16:20:18 +0530169out:
170 free(ssd_cookie);
171 return -1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700172}
173
174int 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 Mittal024c0332010-02-03 11:44:00 -0800198/* 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
229int recovery_init (void)
230{
231 struct recovery_message msg;
Shashank Mittal024c0332010-02-03 11:44:00 -0800232 char partition_name[32];
233 unsigned valid_command = 0;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700234 int update_status = 0;
Shashank Mittal024c0332010-02-03 11:44:00 -0800235
236 // get recovery message
Greg Griscod6250552011-06-29 14:40:23 -0700237 if (get_recovery_message(&msg))
Shashank Mittal024c0332010-02-03 11:44:00 -0800238 return -1;
Shashank Mittal024c0332010-02-03 11:44:00 -0800239 msg.command[sizeof(msg.command)-1] = '\0'; //Ensure termination
Pavel Nedev96a9aea2013-02-26 15:16:26 -0800240 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 Mittal024c0332010-02-03 11:44:00 -0800244
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700245 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 Griscod2471ef2011-07-14 13:00:42 -0700250 int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700251
252 if(!ret && (update_status & 0x01))
253 {
254 dprintf(INFO,"radio update success\n");
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700255 strlcpy(msg.status, "OKAY", sizeof(msg.status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700256 }
257 else
258 {
259 dprintf(INFO,"radio update failed\n");
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700260 strlcpy(msg.status, "failed-update", sizeof(msg.status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700261 }
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700262 strlcpy(msg.command, "", sizeof(msg.command)); // clearing recovery command
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700263 set_recovery_message(&msg); // send recovery message
264 boot_into_recovery = 1; // Boot in recovery mode
265 return 0;
266 }
267
Shashank Mittal024c0332010-02-03 11:44:00 -0800268 valid_command = 1;
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700269 strlcpy(msg.command, "", sizeof(msg.command)); // to safe against multiple reboot into recovery
270 strlcpy(msg.status, "OKAY", sizeof(msg.status));
Shashank Mittal024c0332010-02-03 11:44:00 -0800271 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 Narayanamurthy0e445b02011-06-19 21:34:46 -0700277 dprintf(INFO,"start radio update\n");
Shashank Mittal024c0332010-02-03 11:44:00 -0800278 valid_command = 1;
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700279 strlcpy(partition_name, "FOTA", sizeof(partition_name));
Shashank Mittal024c0332010-02-03 11:44:00 -0800280 }
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 Narayanamurthy0e445b02011-06-19 21:34:46 -0700289 if (set_ssd_radio_update(partition_name)) {
290 /* If writing to FOTA partition fails */
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700291 strlcpy(msg.command, "", sizeof(msg.command));
292 strlcpy(msg.status, "failed-update", sizeof(msg.status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700293 goto SEND_RECOVERY_MSG;
294 }
295 else {
296 /* Setting this to check the radio update status */
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700297 strlcpy(msg.command, "boot-recovery", sizeof(msg.command));
Ajay Dudanif4caa942011-10-02 08:57:15 -0700298 strlcpy(msg.status, "RADIO", sizeof(msg.status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700299 goto SEND_RECOVERY_MSG;
300 }
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700301 strlcpy(msg.status, "OKAY", sizeof(msg.status));
Shashank Mittal024c0332010-02-03 11:44:00 -0800302
303SEND_RECOVERY_MSG:
Shashank Mittal024c0332010-02-03 11:44:00 -0800304 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 Narayanamurthy0e445b02011-06-19 21:34:46 -0700309
310static int emmc_set_recovery_msg(struct recovery_message *out)
311{
312 char *ptn_name = "misc";
313 unsigned long long ptn = 0;
Mayank Grover9db97242017-06-16 12:41:56 +0530314 unsigned blocksize = mmc_get_device_blocksize();
315 unsigned int size = ROUND_TO_PAGE(sizeof(*out), (unsigned)blocksize - 1);
316 unsigned char *data = NULL;
317 int ret = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700318 int index = INVALID_PTN;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700319
Mayank Grover9db97242017-06-16 12:41:56 +0530320 data = malloc(size);
321 if(!data)
322 {
323 dprintf(CRITICAL,"memory allocation error \n");
324 ret = -1;
325 goto out;
326 }
327
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530328 index = partition_get_index((const char *) ptn_name);
Kinson Chikf1a43512011-07-14 11:28:39 -0700329 ptn = partition_get_offset(index);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700330 mmc_set_lun(partition_get_lun(index));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700331 if(ptn == 0) {
332 dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name);
Mayank Grover9db97242017-06-16 12:41:56 +0530333 ret = -1;
334 goto out;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700335 }
Mayank Grover9db97242017-06-16 12:41:56 +0530336 memset(data, 0, size);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700337 memcpy(data, out, sizeof(*out));
338 if (mmc_write(ptn , size, (unsigned int*)data)) {
339 dprintf(CRITICAL,"mmc write failure %s %d\n",ptn_name, sizeof(*out));
Mayank Grover9db97242017-06-16 12:41:56 +0530340 ret = -1;
341 goto out;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700342 }
Mayank Grover9db97242017-06-16 12:41:56 +0530343out:
344 if (data)
345 free(data);
346 return ret;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700347}
348
349static int emmc_get_recovery_msg(struct recovery_message *in)
350{
351 char *ptn_name = "misc";
352 unsigned long long ptn = 0;
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700353 unsigned int size;
Kinson Chikf1a43512011-07-14 11:28:39 -0700354 int index = INVALID_PTN;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700355
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700356 size = mmc_get_device_blocksize();
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530357 index = partition_get_index((const char *) ptn_name);
Channagoud Kadabif2805632015-01-07 16:46:50 -0800358 if (index < 0)
359 {
360 dprintf(CRITICAL, "%s: Partition not found\n", ptn_name);
361 return -1;
362 }
363
Kinson Chikf1a43512011-07-14 11:28:39 -0700364 ptn = partition_get_offset(index);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700365 mmc_set_lun(partition_get_lun(index));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700366 if(ptn == 0) {
367 dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name);
368 return -1;
369 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700370 if (mmc_read(ptn , (unsigned int*)in, size)) {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700371 dprintf(CRITICAL,"mmc read failure %s %d\n",ptn_name, size);
372 return -1;
373 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700374
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700375 return 0;
376}
377
378int _emmc_recovery_init(void)
379{
380 int update_status = 0;
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700381 struct recovery_message *msg;
382 uint32_t block_size = 0;
383
384 block_size = mmc_get_device_blocksize();
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700385
386 // get recovery message
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700387 msg = (struct recovery_message *)memalign(CACHE_LINE, block_size);
388 ASSERT(msg);
389
390 if(emmc_get_recovery_msg(msg))
391 {
392 if(msg)
393 free(msg);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700394 return -1;
Pavel Nedev96a9aea2013-02-26 15:16:26 -0800395 }
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700396
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700397 msg->command[sizeof(msg->command)-1] = '\0'; //Ensure termination
398 if (msg->command[0] != 0 && msg->command[0] != 255) {
399 dprintf(INFO,"Recovery command: %d %s\n",
400 sizeof(msg->command), msg->command);
401 }
402
vijay kumar32e2e3c2014-08-01 21:10:28 +0530403 if (!strcmp(msg->command, "boot-recovery")) {
Stanimir Varbanovbe041992013-04-26 14:29:21 +0300404 boot_into_recovery = 1;
405 }
406
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700407 if (!strcmp("update-radio",msg->command))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700408 {
409 /* We're now here due to radio update, so check for update status */
Greg Griscod2471ef2011-07-14 13:00:42 -0700410 int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700411
412 if(!ret && (update_status & 0x01))
413 {
414 dprintf(INFO,"radio update success\n");
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700415 strlcpy(msg->status, "OKAY", sizeof(msg->status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700416 }
417 else
418 {
419 dprintf(INFO,"radio update failed\n");
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700420 strlcpy(msg->status, "failed-update", sizeof(msg->status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700421 }
Shashank Mittal162244e2011-08-08 19:01:25 -0700422 boot_into_recovery = 1; // Boot in recovery mode
423 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700424 if (!strcmp("reset-device-info",msg->command))
Shashank Mittal162244e2011-08-08 19:01:25 -0700425 {
426 reset_device_info();
427 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700428 if (!strcmp("root-detect",msg->command))
Shashank Mittal162244e2011-08-08 19:01:25 -0700429 {
430 set_device_root();
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700431 }
432 else
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700433 goto out;// do nothing
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700434
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700435 strlcpy(msg->command, "", sizeof(msg->command)); // clearing recovery command
436 emmc_set_recovery_msg(msg); // send recovery message
437
438out:
439 if(msg)
440 free(msg);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700441 return 0;
442}
Pavel Nedeva1e62322013-04-05 15:21:36 +0300443
444static int read_misc(unsigned page_offset, void *buf, unsigned size)
445{
446 const char *ptn_name = "misc";
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700447 uint32_t pagesize = get_page_size();
Pavel Nedeva1e62322013-04-05 15:21:36 +0300448 unsigned offset;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300449
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700450 if (size == 0 || buf == NULL)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300451 return -1;
452
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700453 offset = page_offset * pagesize;
454
Pavel Nedeva1e62322013-04-05 15:21:36 +0300455 if (target_is_emmc_boot())
456 {
457 int index;
458 unsigned long long ptn;
459 unsigned long long ptn_size;
460
461 index = partition_get_index(ptn_name);
462 if (index == INVALID_PTN)
463 {
464 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
465 return -1;
466 }
467
468 ptn = partition_get_offset(index);
469 ptn_size = partition_get_size(index);
470
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700471 mmc_set_lun(partition_get_lun(index));
472
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700473 if (ptn_size < offset + size)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300474 {
475 dprintf(CRITICAL, "Read request out of '%s' boundaries\n",
476 ptn_name);
477 return -1;
478 }
479
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700480 if (mmc_read(ptn + offset, (unsigned int *)buf, size))
Pavel Nedeva1e62322013-04-05 15:21:36 +0300481 {
482 dprintf(CRITICAL, "Reading MMC failed\n");
483 return -1;
484 }
485 }
486 else
487 {
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700488 dprintf(CRITICAL, "Misc partition not supported for NAND targets.\n");
489 return -1;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300490 }
491
Pavel Nedeva1e62322013-04-05 15:21:36 +0300492 return 0;
493}
494
Pavel Nedevc728bb32013-04-05 16:58:05 +0300495int write_misc(unsigned page_offset, void *buf, unsigned size)
496{
497 const char *ptn_name = "misc";
498 void *scratch_addr = target_get_scratch_address();
499 unsigned offset;
500 unsigned aligned_size;
501
502 if (size == 0 || buf == NULL || scratch_addr == NULL)
503 return -1;
504
505 if (target_is_emmc_boot())
506 {
507 int index;
508 unsigned long long ptn;
509 unsigned long long ptn_size;
510
511 index = partition_get_index(ptn_name);
512 if (index == INVALID_PTN)
513 {
514 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
515 return -1;
516 }
517
518 ptn = partition_get_offset(index);
519 ptn_size = partition_get_size(index);
520
521 offset = page_offset * BLOCK_SIZE;
522 aligned_size = ROUND_TO_PAGE(size, (unsigned)BLOCK_SIZE - 1);
523 if (ptn_size < offset + aligned_size)
524 {
525 dprintf(CRITICAL, "Write request out of '%s' boundaries\n",
526 ptn_name);
527 return -1;
528 }
529
Mayank Grover467bf492017-10-24 12:27:53 +0530530 /* This will ensure, we zeored out any extra bytes
531 we will push to emmc, to prevent information leak */
532 if (aligned_size > size)
533 memset((scratch_addr + size), 0, (aligned_size-size));
534
Pavel Nedevc728bb32013-04-05 16:58:05 +0300535 if (scratch_addr != buf)
536 memcpy(scratch_addr, buf, size);
Channagoud Kadabi2776b652015-08-28 15:41:26 -0700537
538 /* Set Lun for misc partition */
539 mmc_set_lun(partition_get_lun(index));
540
Pavel Nedevc728bb32013-04-05 16:58:05 +0300541 if (mmc_write(ptn + offset, aligned_size, (unsigned int *)scratch_addr))
542 {
543 dprintf(CRITICAL, "Writing MMC failed\n");
544 return -1;
545 }
546 }
547 else
548 {
549 struct ptentry *ptn;
550 struct ptable *ptable;
551 unsigned pagesize = flash_page_size();
552
553 ptable = flash_get_ptable();
554 if (ptable == NULL)
555 {
556 dprintf(CRITICAL, "Partition table not found\n");
557 return -1;
558 }
559
560 ptn = ptable_find(ptable, ptn_name);
561 if (ptn == NULL)
562 {
563 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
564 return -1;
565 }
566
567 offset = page_offset * pagesize;
568 aligned_size = ROUND_TO_PAGE(size, pagesize - 1);
569 if (ptn->length < offset + aligned_size)
570 {
571 dprintf(CRITICAL, "Write request out of '%s' boundaries\n",
572 ptn_name);
573 return -1;
574 }
575
Mayank Grover467bf492017-10-24 12:27:53 +0530576 /* This will ensure, we zeored out any extra bytes
577 we will push, to prevent information leak */
578 if (aligned_size > size)
579 memset((scratch_addr + size), 0, (aligned_size-size));
580
Pavel Nedevc728bb32013-04-05 16:58:05 +0300581 if (scratch_addr != buf)
582 memcpy(scratch_addr, buf, size);
Mayank Grover467bf492017-10-24 12:27:53 +0530583
Pavel Nedevc728bb32013-04-05 16:58:05 +0300584 if (flash_write(ptn, offset, scratch_addr, aligned_size)) {
585 dprintf(CRITICAL, "Writing flash failed\n");
586 return -1;
587 }
588 }
589
590 return 0;
591}
592
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700593int get_ffbm(char *ffbm, unsigned size)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300594{
595 const char *ffbm_cmd = "ffbm-";
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700596 uint32_t page_size = get_page_size();
597 char *ffbm_page_buffer = NULL;
598 int retval = 0;
599 if (size < FFBM_MODE_BUF_SIZE || size >= page_size)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300600 {
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700601 dprintf(CRITICAL, "Invalid size argument passed to get_ffbm\n");
602 retval = -1;
603 goto cleanup;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300604 }
Parth Dixit75b16812016-04-05 19:17:17 +0530605 ffbm_page_buffer = (char*)memalign(CACHE_LINE, page_size);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700606 if (!ffbm_page_buffer)
607 {
608 dprintf(CRITICAL, "Failed to alloc buffer for ffbm cookie\n");
609 retval = -1;
610 goto cleanup;
611 }
612 if (read_misc(0, ffbm_page_buffer, page_size))
Pavel Nedeva1e62322013-04-05 15:21:36 +0300613 {
614 dprintf(CRITICAL, "Error reading MISC partition\n");
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700615 retval = -1;
616 goto cleanup;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300617 }
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700618 ffbm_page_buffer[size] = '\0';
619 if (strncmp(ffbm_cmd, ffbm_page_buffer, strlen(ffbm_cmd)))
620 {
621 retval = 0;
622 goto cleanup;
623 }
624 else
625 {
626 if (strlcpy(ffbm, ffbm_page_buffer, size) <
627 FFBM_MODE_BUF_SIZE -1)
628 {
629 dprintf(CRITICAL, "Invalid string in misc partition\n");
630 retval = -1;
631 }
632 else
633 retval = 1;
634 }
635cleanup:
636 if(ffbm_page_buffer)
637 free(ffbm_page_buffer);
638 return retval;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300639}
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700640
641