2 #include "normal_mode.h"
3 #include "../disk/part_uefi.h"
4 #include "../disk/part_efi.h"
5 #include "../drivers/mmc/card_sdio.h"
6 #include "asm/arch/sci_types.h"
7 #include <ext_common.h>
9 #ifdef CONFIG_SECURE_BOOT
10 #include <asm/arch/secure_boot.h>
11 #include "secure_verify.h"
13 #include <asm/arch/sprd_reg.h>
14 #ifdef CONFIG_ARCH_SCX35L //only for sharkL branch modem boot process
15 #include <asm/arch/cp_boot.h>
20 #define KERNL_PAGE_SIZE 2048
22 long long load_image_time = 0;
24 #ifdef CONFIG_SUPPORT_TDLTE
25 static boot_image_required_t const s_boot_image_tl_table[] = {
26 {L"tl_fixnv1", L"tl_fixnv2", LTE_FIXNV_SIZE, LTE_FIXNV_ADDR},
27 {L"tl_runtimenv1", L"tl_runtimenv2", LTE_RUNNV_SIZE, LTE_RUNNV_ADDR},
28 {L"tl_modem", NULL, LTE_MODEM_SIZE, LTE_MODEM_ADDR},
29 {L"tl_ldsp", NULL, LTE_LDSP_SIZE, LTE_LDSP_ADDR}, //ltedsp
30 {L"tl_tgdsp", NULL, LTE_GDSP_SIZE, LTE_GDSP_ADDR},
35 #ifdef CONFIG_SUPPORT_WLTE
36 static boot_image_required_t const s_boot_image_wl_table[] = {
37 {L"wl_fixnv1", L"wl_fixnv2", LTE_FIXNV_SIZE, LTE_FIXNV_ADDR},
38 {L"wl_runtimenv1", L"wl_runtimenv2", LTE_RUNNV_SIZE, LTE_RUNNV_ADDR},
39 {L"wl_modem", NULL, LTE_MODEM_SIZE, LTE_MODEM_ADDR},
40 {L"wl_ldsp", NULL, LTE_LDSP_SIZE, LTE_LDSP_ADDR},
41 {L"wl_gdsp", NULL, LTE_GDSP_SIZE, LTE_GDSP_ADDR},
42 {L"wl_warm", NULL, WL_WARM_SIZE, WL_WARM_ADDR},
47 #ifdef CONFIG_SUPPORT_LTE
48 static boot_image_required_t const s_boot_image_lte_table[] = {
49 {L"l_fixnv1", L"l_fixnv2", LTE_FIXNV_SIZE, LTE_FIXNV_ADDR},
50 {L"l_runtimenv1", L"l_runtimenv2", LTE_RUNNV_SIZE, LTE_RUNNV_ADDR},
51 {L"l_modem", NULL, LTE_MODEM_SIZE, LTE_MODEM_ADDR},
52 {L"l_ldsp", NULL, LTE_LDSP_SIZE, LTE_LDSP_ADDR},
53 {L"l_gdsp", NULL, LTE_GDSP_SIZE, LTE_GDSP_ADDR},
54 {L"l_warm", NULL, WL_WARM_SIZE, WL_WARM_ADDR},
59 #ifdef CONFIG_SUPPORT_TD
60 static boot_image_required_t const s_boot_image_TD_table[] = {
61 {L"tdfixnv1", L"tdfixnv2", FIXNV_SIZE, TDFIXNV_ADR},
62 {L"tdruntimenv1", L"tdruntimenv2", RUNTIMENV_SIZE, TDRUNTIMENV_ADR},
63 {L"tdmodem", NULL, TDMODEM_SIZE, TDMODEM_ADR},
64 {L"tddsp", NULL, TDDSP_SIZE, TDDSP_ADR},
69 #ifdef CONFIG_SUPPORT_GSM
70 static boot_image_required_t const s_boot_image_gsm_table[] = {
71 {L"g_fixnv1", L"g_fixnv2", GSM_FIXNV_SIZE, GSM_FIXNV_ADDR},
72 {L"g_runtimenv1", L"g_runtimenv2", GSM_RUNNV_SIZE, GSM_RUNNV_ADDR},
73 {L"g_modem", NULL, GSM_MODEM_SIZE, GSM_MODEM_ADDR},
74 {L"g_dsp", NULL, GSM_DSP_SIZE, GSM_DSP_ADDR},
79 #ifdef CONFIG_SUPPORT_W
80 static boot_image_required_t const s_boot_image_W_table[] = {
81 {L"wfixnv1", L"wfixnv2", FIXNV_SIZE, WFIXNV_ADR},
82 {L"wruntimenv1", L"wruntimenv2", RUNTIMENV_SIZE, WRUNTIMENV_ADR},
83 {L"wmodem", NULL, WMODEM_SIZE, WMODEM_ADR},
84 {L"wdsp", NULL, WDSP_SIZE, WDSP_ADR},
89 #ifdef CONFIG_SUPPORT_WIFI
90 static boot_image_required_t const s_boot_image_WIFI_table[] = {
91 {L"wcnfixnv1", L"wcnfixnv2", FIXNV_SIZE, WCNFIXNV_ADR},
92 {L"wcnruntimenv1", L"wcnruntimenv2", RUNTIMENV_SIZE, WCNRUNTIMENV_ADR},
93 {L"wcnmodem", NULL, WCNMODEM_SIZE, WCNMODEM_ADR},
98 static boot_image_required_t const s_boot_image_COMMON_table[] = {
99 #if !BOOT_NATIVE_LINUX
100 {L"vm", NULL, VMJALUNA_SIZE, VMJALUNA_ADR},
102 #ifdef CONFIG_SIMLOCK_ENABLE
103 {L"simlock", NULL, SIMLOCK_SIZE, SIMLOCK_ADR},
105 #ifdef CONFIG_DFS_ENABLE
106 {L"pm_sys", NULL, DFS_SIZE, DFS_ADDR},
112 static boot_image_required_t *const s_boot_image_table[] = {
113 #ifdef CONFIG_SUPPORT_TDLTE
114 s_boot_image_tl_table,
117 #ifdef CONFIG_SUPPORT_WLTE
118 s_boot_image_wl_table,
121 #ifdef CONFIG_SUPPORT_LTE
122 s_boot_image_lte_table,
125 #ifdef CONFIG_SUPPORT_GSM
126 s_boot_image_gsm_table,
129 #ifdef CONFIG_SUPPORT_TD
130 s_boot_image_TD_table,
134 #ifdef CONFIG_SUPPORT_W
135 s_boot_image_W_table,
140 #ifdef CONFIG_SUPPORT_WIFI
141 s_boot_image_WIFI_table,
144 s_boot_image_COMMON_table,
149 #ifdef CONFIG_SECURE_BOOT
150 uint8 header_buf[SEC_HEADER_MAX_SIZE];
153 int read_logoimg(char *bmp_img, size_t size)
155 block_dev_desc_t *p_block_dev = NULL;
156 disk_partition_t info;
158 p_block_dev = get_dev("mmc", 1);
159 if (NULL == p_block_dev) {
162 if (!get_partition_info_by_name(p_block_dev, L"logo", &info)) {
163 if (TRUE != Emmc_Read(PARTITION_USER, info.start, size / EMMC_SECTOR_SIZE, bmp_img)) {
164 debugf("function: %s nand read error\n", __FUNCTION__);
174 int size = CONFIG_SPL_LOAD_LEN;
175 if (TRUE != Emmc_Read(PARTITION_BOOT1, 0, size / EMMC_SECTOR_SIZE, (uint8 *) spl_data)) {
176 debugf("vmjaluna nand read error \n");
183 #ifdef CONFIG_SECURE_BOOT
185 int get_spl_hash(void *hash_data)
191 //int size = CONFIG_SPL_LOAD_LEN;
192 int size = CONFIG_SPL_HASH_LEN;
194 spl_data = malloc(size);
199 if (TRUE != Emmc_Read(PARTITION_BOOT1, 0, size / EMMC_SECTOR_SIZE, (uint8 *) spl_data)) {
200 debugf("PARTITION_BOOT1 read error \n");
204 header = (NBLHeader *) ((unsigned char *)spl_data + BOOTLOADER_HEADER_OFFSET);
205 len = header->mHashLen;
207 memset(header, 0, sizeof(NBLHeader));
208 header->mHashLen = len;
209 debugf("cal spl hash len=%d\n", header->mHashLen * 4);
211 ret = cal_sha1(spl_data, (header->mHashLen) << 2, hash_data);
221 just convert partition name wchar to char with violent.
223 LOCAL __inline char *w2c(wchar_t * wchar)
225 static char buf[72] = { 0 };
227 while ((NULL != wchar[i]) && (i < 72)) {
228 buf[i] = wchar[i] & 0xFF;
236 LOCAL void _boot_secure_check(void)
238 #ifdef CONFIG_SECURE_BOOT
240 vlr_info_t *vlr_info;
242 #ifdef CONFIG_SUPPORT_W
243 puk_adr = CONFIG_SYS_NAND_U_BOOT_DST + CONFIG_SYS_NAND_U_BOOT_SIZE - KEY_INFO_SIZ - VLR_INFO_SIZ;
244 vlr_info = (vlr_info_t *) (WDSP_ADR + WDSP_SIZE - VLR_INFO_SIZ);
245 secure_check(WDSP_ADR, vlr_info->length, vlr_info, puk_adr);
247 puk_adr = CONFIG_SYS_NAND_U_BOOT_DST + CONFIG_SYS_NAND_U_BOOT_SIZE - KEY_INFO_SIZ - VLR_INFO_SIZ;
248 vlr_info = (vlr_info_t *) (WMODEM_ADR + WMODEM_SIZE - VLR_INFO_SIZ);
249 secure_check(WMODEM_ADR, vlr_info->length, vlr_info, puk_adr);
252 #ifdef CONFIG_SUPPORT_WIFI
253 puk_adr = CONFIG_SYS_NAND_U_BOOT_DST + CONFIG_SYS_NAND_U_BOOT_SIZE - KEY_INFO_SIZ - VLR_INFO_SIZ;
254 vlr_info = (vlr_info_t *) (WCNMODEM_ADR + WCNMODEM_SIZE - VLR_INFO_SIZ);
255 secure_check(WCNMODEM_ADR, vlr_info->length, vlr_info, puk_adr);
258 #if !BOOT_NATIVE_LINUX
259 secure_check(VMJALUNA_ADR, 0, VMJALUNA_ADR + VMJALUNA_SIZE - VLR_INFO_OFF,
260 CONFIG_SYS_NAND_U_BOOT_DST + CONFIG_SYS_NAND_U_BOOT_SIZE - KEY_INFO_SIZ - VLR_INFO_OFF);
263 #ifdef CONFIG_SIMLOCK
264 secure_check(SIMLOCK_ADR, 0, SIMLOCK_ADR + SIMLOCK_SIZE - VLR_INFO_OFF,
265 CONFIG_SYS_NAND_U_BOOT_DST + CONFIG_SYS_NAND_U_BOOT_SIZE - KEY_INFO_SIZ - VLR_INFO_OFF);
272 Function for reading user partition.
274 PUBLIC int _boot_partition_read(block_dev_desc_t * dev, wchar_t * partition_name, u32 offsetsector, u32 size, u8 * buf)
280 disk_partition_t info;
283 debugf("%s:buf is NULL!\n", __func__);
286 nsct = size / EMMC_SECTOR_SIZE;
287 left = size % EMMC_SECTOR_SIZE;
289 if (get_partition_info_by_name(dev, partition_name, &info)) {
290 debugf("get partition %s info failed!\n", w2c(partition_name));
294 if (TRUE != Emmc_Read(PARTITION_USER, info.start + offsetsector, nsct, buf))
298 sctbuf = malloc(EMMC_SECTOR_SIZE);
299 if (NULL != sctbuf) {
300 if (TRUE == Emmc_Read(PARTITION_USER, info.start + offsetsector + nsct, 1, sctbuf)) {
301 memcpy(buf + (nsct * EMMC_SECTOR_SIZE), sctbuf, left);
311 debugf("%s: partition %s read %s!\n", __func__, w2c(partition_name), ret ? "success" : "failed");
315 PUBLIC int blk_data_read(u32 offset, u32 size, u8 *buf)
319 u32 start_sec, nsct, left;
322 debugf("NULL BUF\n");
326 start_sec = offset / EMMC_SECTOR_SIZE;
327 nsct = size / EMMC_SECTOR_SIZE;
328 left = size % EMMC_SECTOR_SIZE;
330 if (nsct && !Emmc_Read(PARTITION_USER, start_sec, nsct, buf)) {
331 debugf("Failed to read mmc\n");
336 sctbuf = malloc(EMMC_SECTOR_SIZE);
338 if (!Emmc_Read(PARTITION_USER, start_sec + nsct, 1, sctbuf)) {
339 debugf("Failed to read mmc\n");
343 memcpy(buf + (nsct * EMMC_SECTOR_SIZE), sctbuf, left);
356 Function for writing user partition.
358 PUBLIC int _boot_partition_write(block_dev_desc_t * dev, wchar_t * partition_name, u32 size, u8 * buf)
360 disk_partition_t info;
363 debugf("%s:buf is NULL!\n", __FUNCTION__);
366 size = (size + (EMMC_SECTOR_SIZE - 1)) & (~(EMMC_SECTOR_SIZE - 1));
367 size = size / EMMC_SECTOR_SIZE;
368 if (0 == get_partition_info_by_name(dev, partition_name, &info)) {
369 if (TRUE != Emmc_Write(PARTITION_USER, info.start, size, buf)) {
370 debugf("%s: partition:%s read error!\n", __FUNCTION__, w2c(partition_name));
374 debugf("%s: partition:%s >>>get partition info failed!\n", __FUNCTION__, w2c(partition_name));
377 debugf("%s: partition:%s write success!\n", __FUNCTION__, w2c(partition_name));
382 Function for displaying logo.
384 LOCAL __inline void _boot_display_logo(block_dev_desc_t * dev, int backlight_set)
388 #if defined(CONFIG_LCD_720P) || defined(CONFIG_LCD_HD) || CONFIG_LCD_PAD_WXGA //LiWei add CONFIG_LCD_HD
393 uint8 *bmp_img = malloc(size);
395 debugf("%s: malloc for splash image failed!\n", __FUNCTION__);
398 if (!_boot_partition_read(dev, L"logo", 0, size, bmp_img)) {
399 debugf("%s: read logo partition failed!\n", __FUNCTION__);
402 lcd_display_logo(backlight_set, (ulong) bmp_img, size);
409 we assume partition with backup must check ecc.
411 LOCAL __inline int _boot_read_partition_with_backup(block_dev_desc_t * dev, boot_image_required_t info)
413 uint8 *bakbuf = NULL;
414 uint8 *oribuf = NULL;
416 uint8 header[EMMC_SECTOR_SIZE];
418 nv_header_t *header_p = NULL;
419 uint32 bufsize = info.size + EMMC_SECTOR_SIZE;
422 bakbuf = malloc(bufsize);
425 memset(bakbuf, 0xff, bufsize);
426 oribuf = malloc(bufsize);
427 if (NULL == oribuf) {
431 memset(oribuf, 0xff, bufsize);
433 if (_boot_partition_read(dev, info.partition, 0, info.size + EMMC_SECTOR_SIZE, oribuf)) {
434 memset(header, 0, EMMC_SECTOR_SIZE);
435 memcpy(header, oribuf, EMMC_SECTOR_SIZE);
436 checksum = header_p->checksum;
437 debugf("_boot_read_partition_with_backup origin checksum 0x%x\n", checksum);
438 if (_chkNVEcc(oribuf + EMMC_SECTOR_SIZE, info.size, checksum)) {
439 memcpy(info.mem_addr, oribuf + EMMC_SECTOR_SIZE, info.size);
443 if (_boot_partition_read(dev, info.bak_partition, 0, info.size + EMMC_SECTOR_SIZE, bakbuf)) {
444 memset(header, 0, EMMC_SECTOR_SIZE);
445 memcpy(header, bakbuf, EMMC_SECTOR_SIZE);
446 checksum = header_p->checksum;
447 debugf("_boot_read_partition_with_backup backup checksum 0x%x\n", checksum);
448 if (_chkNVEcc(bakbuf + EMMC_SECTOR_SIZE, info.size, checksum))
454 debugf("%s:(%s)both org and bak partition are damaged!\n", __FUNCTION__, w2c(info.partition));
455 memset(info.mem_addr, 0, info.size);
460 debugf("%s:(%s)bak partition is damaged!\n", __FUNCTION__, w2c(info.bak_partition));
461 _boot_partition_write(dev, info.bak_partition, info.size + EMMC_SECTOR_SIZE, oribuf);
464 debugf("%s:(%s)org partition is damaged!\n!", __FUNCTION__, w2c(info.partition));
465 memcpy(info.mem_addr, bakbuf + EMMC_SECTOR_SIZE, info.size);
466 _boot_partition_write(dev, info.partition, info.size + EMMC_SECTOR_SIZE, bakbuf);
469 debugf("%s:(%s)both org and bak partition are ok!\n", __FUNCTION__, w2c(info.partition));
472 debugf("%s: status error!\n", __FUNCTION__);
483 Function for reading image which is needed when power on.
486 int _boot_load_required_image(block_dev_desc_t * dev, boot_image_required_t img_info)
488 uint32 secure_boot_offset = 0;
490 debugf("%s: load %s to addr 0x%08x\n", __FUNCTION__, w2c(img_info.partition), img_info.mem_addr);
492 if (NULL != img_info.bak_partition) {
493 _boot_read_partition_with_backup(dev, img_info);
495 #ifdef CONFIG_SECURE_BOOT
496 if (!_boot_partition_read(dev, img_info.partition, 0, SEC_HEADER_MAX_SIZE, header_buf)) {
497 debugf("%s:%s read error!\n", __FUNCTION__, w2c(img_info.partition));
500 //if(header_parser(header_buf) )
501 secure_boot_offset = get_code_offset(header_buf);
502 _boot_partition_read(dev, img_info.partition, 0 + secure_boot_offset, img_info.size, (u8 *) img_info.mem_addr);
504 secure_verify(L"uboot", header_buf, img_info.mem_addr);
506 _boot_partition_read(dev, img_info.partition, 0, img_info.size, (u8 *) img_info.mem_addr);
514 Function for checking and loading kernel/ramdisk image.
516 LOCAL int _boot_load_kernel_ramdisk_image(block_dev_desc_t * dev, char *bootmode, boot_img_hdr * hdr)
518 wchar_t *partition = NULL;
521 uint32 secure_boot_offset = 0;
522 disk_partition_t info;
524 if (0 == memcmp(bootmode, RECOVERY_PART, strlen(RECOVERY_PART))) {
525 partition = L"recovery";
526 debugf("enter recovery mode!\n");
527 set_recovery_mode(1);
529 partition = L""BOOT_PART;
530 debugf("Enter boot mode (partition name: %s)\n", w2c(partition));
533 if (get_partition_info_by_name(dev, partition, &info)) {
534 debugf("get partition %s info failed!\n", w2c(partition));
538 offset = info.start * info.blksz;
540 if (!blk_data_read(offset, sizeof(*hdr), hdr)) {
541 debugf("%s:%s read error!\n", __FUNCTION__, w2c(partition));
546 if (hdr->magic_num != MAGIC_NUM) {
547 debugf("BAD BOOT IMAGE HEADER: %x\n", hdr->magic_num);
550 debugf("BOOT IMAGE HEADER: %x\n", hdr->magic_num);
553 size = roundup(sizeof(*hdr), ALIGN_SIZE) +
554 roundup(hdr->kernel_size, ALIGN_SIZE) +
555 roundup(hdr->dt_size, ALIGN_SIZE);
557 debugf("bzImage size: %x\n", size);
559 if (!blk_data_read(offset, size, KERNEL_ADR - roundup(sizeof(*hdr), ALIGN_SIZE))) {
560 debugf("%s:%s kernel read error!\n", __FUNCTION__, w2c(partition));
565 dt_img_adr = KERNEL_ADR + roundup(hdr->kernel_size, ALIGN_SIZE);
566 debugf("dt_img_adr: %u\n", dt_img_adr);
567 debugf("dt_img_adr: %x\n", dt_img_adr);
568 if (load_dtb((int)DT_ADR, (void *)dt_img_adr)) {
569 debugf("%s:dt load error!\n", __FUNCTION__);
575 #ifdef CONFIG_RAMDISK_BOOT
578 if (0 == memcmp(bootmode, RECOVERY_PART, strlen(RECOVERY_PART)))
579 ramdisk_part = PARTS_RAMDISK2;
581 ramdisk_part = PARTS_RAMDISK;
582 load_ramdisk(ramdisk_part, RAMDISK_ADR, RAMDISK_SIZE_MB * 1024 * 1024);
584 #endif /* CONFIG_RAMDISK_BOOT */
585 #else /* CONFIG_TIZEN */
586 #ifdef CONFIG_SDRAMDISK
588 int sd_ramdisk_size = 0;
590 size = WDSP_ADR - RAMDISK_ADR;
592 size = TDDSP_ADR - RAMDISK_ADR;
595 sd_ramdisk_size = load_sd_ramdisk((uint8 *) RAMDISK_ADR, size);
596 if (sd_ramdisk_size > 0)
597 hdr->ramdisk_size = sd_ramdisk_size;
600 #endif /* CONFIG_TIZEN */
604 #ifdef CONFIG_SECURE_BOOT
605 PUBLIC int secure_verify_partition(block_dev_desc_t * dev, wchar_t * partition_name, int ram_addr)
609 disk_partition_t info;
611 if (get_partition_info_by_name(dev, partition_name, &info)) {
612 debugf("verify get partition %s info failed!\n", w2c(partition_name));
615 size = info.size * GPT_BLOCK_SIZE;
616 debugf("%s=%x =%x\n", w2c(partition_name), info.size, size);
618 _boot_partition_read(dev, partition_name, 0, size, (u8 *) ram_addr);
619 secure_verify(L"uboot", ram_addr, 0);
624 void vlx_nand_boot(char *kernel_pname, char *cmdline, int backlight_set)
626 boot_img_hdr *hdr = (void *)raw_header;
627 block_dev_desc_t *dev = NULL;
628 wchar_t *partition = NULL;
630 long long start = get_ticks();
632 dev = get_dev("mmc", 1);
634 debugf("Fatal Error,get_dev mmc failed!\n");
638 pmic_arm7_RAM_active();
642 #ifdef CONFIG_SPLASH_SCREEN
643 _boot_display_logo(dev, backlight_set);
644 performance_debug("7:");
649 #if ((!BOOT_NATIVE_LINUX)||(BOOT_NATIVE_LINUX_MODEM))
650 //load required image which config in table
652 while (s_boot_image_table[i]) {
654 while (s_boot_image_table[i][j].partition) {
655 _boot_load_required_image(dev, s_boot_image_table[i][j]);
660 performance_debug("8:");
663 #ifdef CONFIG_SECURE_BOOT
664 if (0 == memcmp(kernel_pname, RECOVERY_PART, strlen(RECOVERY_PART))) {
665 partition = L"recovery";
669 secure_verify_partition(dev, partition, KERNEL_ADR);
671 //loader kernel and ramdisk
672 if (!_boot_load_kernel_ramdisk_image(dev, kernel_pname, hdr))
674 performance_debug("9:");
675 load_image_time = get_ticks() - start;
676 //secure check for secure boot
677 //_boot_secure_check();
679 if (creat_cmdline(cmdline, hdr)) {
680 debugf("creat_cmdline failed\n");
683 #if BOOT_NATIVE_LINUX_MODEM
687 #if defined CONFIG_SC9630