blob: f5138f377f8edd8aff185ddf4fd632a5584b20ea [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;
314 unsigned int size = ROUND_TO_PAGE(sizeof(*out),511);
315 unsigned char data[size];
Kinson Chikf1a43512011-07-14 11:28:39 -0700316 int index = INVALID_PTN;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700317
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530318 index = partition_get_index((const char *) ptn_name);
Kinson Chikf1a43512011-07-14 11:28:39 -0700319 ptn = partition_get_offset(index);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700320 mmc_set_lun(partition_get_lun(index));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700321 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
333static int emmc_get_recovery_msg(struct recovery_message *in)
334{
335 char *ptn_name = "misc";
336 unsigned long long ptn = 0;
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700337 unsigned int size;
Kinson Chikf1a43512011-07-14 11:28:39 -0700338 int index = INVALID_PTN;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700339
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700340 size = mmc_get_device_blocksize();
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530341 index = partition_get_index((const char *) ptn_name);
Channagoud Kadabif2805632015-01-07 16:46:50 -0800342 if (index < 0)
343 {
344 dprintf(CRITICAL, "%s: Partition not found\n", ptn_name);
345 return -1;
346 }
347
Kinson Chikf1a43512011-07-14 11:28:39 -0700348 ptn = partition_get_offset(index);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700349 mmc_set_lun(partition_get_lun(index));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700350 if(ptn == 0) {
351 dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name);
352 return -1;
353 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700354 if (mmc_read(ptn , (unsigned int*)in, size)) {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700355 dprintf(CRITICAL,"mmc read failure %s %d\n",ptn_name, size);
356 return -1;
357 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700358
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700359 return 0;
360}
361
362int _emmc_recovery_init(void)
363{
364 int update_status = 0;
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700365 struct recovery_message *msg;
366 uint32_t block_size = 0;
367
368 block_size = mmc_get_device_blocksize();
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700369
370 // get recovery message
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700371 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 Narayanamurthy0e445b02011-06-19 21:34:46 -0700378 return -1;
Pavel Nedev96a9aea2013-02-26 15:16:26 -0800379 }
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700380
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700381 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 kumar32e2e3c2014-08-01 21:10:28 +0530387 if (!strcmp(msg->command, "boot-recovery")) {
Stanimir Varbanovbe041992013-04-26 14:29:21 +0300388 boot_into_recovery = 1;
389 }
390
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700391 if (!strcmp("update-radio",msg->command))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700392 {
393 /* We're now here due to radio update, so check for update status */
Greg Griscod2471ef2011-07-14 13:00:42 -0700394 int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700395
396 if(!ret && (update_status & 0x01))
397 {
398 dprintf(INFO,"radio update success\n");
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700399 strlcpy(msg->status, "OKAY", sizeof(msg->status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700400 }
401 else
402 {
403 dprintf(INFO,"radio update failed\n");
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700404 strlcpy(msg->status, "failed-update", sizeof(msg->status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700405 }
Shashank Mittal162244e2011-08-08 19:01:25 -0700406 boot_into_recovery = 1; // Boot in recovery mode
407 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700408 if (!strcmp("reset-device-info",msg->command))
Shashank Mittal162244e2011-08-08 19:01:25 -0700409 {
410 reset_device_info();
411 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700412 if (!strcmp("root-detect",msg->command))
Shashank Mittal162244e2011-08-08 19:01:25 -0700413 {
414 set_device_root();
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700415 }
416 else
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700417 goto out;// do nothing
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700418
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700419 strlcpy(msg->command, "", sizeof(msg->command)); // clearing recovery command
420 emmc_set_recovery_msg(msg); // send recovery message
421
422out:
423 if(msg)
424 free(msg);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700425 return 0;
426}
Pavel Nedeva1e62322013-04-05 15:21:36 +0300427
428static int read_misc(unsigned page_offset, void *buf, unsigned size)
429{
430 const char *ptn_name = "misc";
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700431 uint32_t pagesize = get_page_size();
Pavel Nedeva1e62322013-04-05 15:21:36 +0300432 unsigned offset;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300433
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700434 if (size == 0 || buf == NULL)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300435 return -1;
436
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700437 offset = page_offset * pagesize;
438
Pavel Nedeva1e62322013-04-05 15:21:36 +0300439 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 Kadabi5d0371c2014-10-21 22:27:07 -0700455 mmc_set_lun(partition_get_lun(index));
456
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700457 if (ptn_size < offset + size)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300458 {
459 dprintf(CRITICAL, "Read request out of '%s' boundaries\n",
460 ptn_name);
461 return -1;
462 }
463
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700464 if (mmc_read(ptn + offset, (unsigned int *)buf, size))
Pavel Nedeva1e62322013-04-05 15:21:36 +0300465 {
466 dprintf(CRITICAL, "Reading MMC failed\n");
467 return -1;
468 }
469 }
470 else
471 {
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700472 dprintf(CRITICAL, "Misc partition not supported for NAND targets.\n");
473 return -1;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300474 }
475
Pavel Nedeva1e62322013-04-05 15:21:36 +0300476 return 0;
477}
478
Pavel Nedevc728bb32013-04-05 16:58:05 +0300479int 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 Kadabi2776b652015-08-28 15:41:26 -0700516
517 /* Set Lun for misc partition */
518 mmc_set_lun(partition_get_lun(index));
519
Pavel Nedevc728bb32013-04-05 16:58:05 +0300520 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 Dinamani41fa8d62013-05-23 13:25:36 -0700566int get_ffbm(char *ffbm, unsigned size)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300567{
568 const char *ffbm_cmd = "ffbm-";
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700569 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 Nedeva1e62322013-04-05 15:21:36 +0300573 {
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700574 dprintf(CRITICAL, "Invalid size argument passed to get_ffbm\n");
575 retval = -1;
576 goto cleanup;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300577 }
Parth Dixit75b16812016-04-05 19:17:17 +0530578 ffbm_page_buffer = (char*)memalign(CACHE_LINE, page_size);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700579 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 Nedeva1e62322013-04-05 15:21:36 +0300586 {
587 dprintf(CRITICAL, "Error reading MISC partition\n");
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700588 retval = -1;
589 goto cleanup;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300590 }
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700591 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 }
608cleanup:
609 if(ffbm_page_buffer)
610 free(ffbm_page_buffer);
611 return retval;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300612}
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700613
614