common: rkimg: clean up code

Change-Id: I392fce121353b6ae51fcd016b3ba5cedeb033757
Signed-off-by: Joseph Chen <chenjh@rock-chips.com>
This commit is contained in:
Joseph Chen 2019-07-10 16:03:28 +08:00 committed by Jianhong Chen
parent 3f6d16ab3d
commit d14162bd80
1 changed files with 59 additions and 76 deletions

View File

@ -5,28 +5,26 @@
*/ */
#include <common.h> #include <common.h>
#include <bidram.h>
#include <bootm.h> #include <bootm.h>
#include <mmc.h> #include <boot_rkimg.h>
#include <linux/list.h> #include <console.h>
#include <linux/libfdt.h>
#include <malloc.h> #include <malloc.h>
#include <mmc.h>
#include <part.h>
#include <sysmem.h>
#include <asm/io.h>
#include <linux/libfdt.h>
#include <asm/arch/hotkey.h> #include <asm/arch/hotkey.h>
#include <asm/arch/resource_img.h> #include <asm/arch/resource_img.h>
#include <asm/arch/rockchip_crc.h> #include <asm/arch/rockchip_crc.h>
#include <boot_rkimg.h>
#include <asm/arch/boot_mode.h> #include <asm/arch/boot_mode.h>
#include <asm/io.h>
#include <part.h>
#include <bidram.h>
#include <console.h>
#include <sysmem.h>
#define TAG_KERNEL 0x4C4E524B
#define DTB_FILE "rk-kernel.dtb"
#define TAG_KERNEL 0x4C4E524B
#define DTB_FILE "rk-kernel.dtb"
#define BOOTLOADER_MESSAGE_OFFSET_IN_MISC (16 * 1024) #define BOOTLOADER_MESSAGE_OFFSET_IN_MISC (16 * 1024)
#define BOOTLOADER_MESSAGE_BLK_OFFSET (BOOTLOADER_MESSAGE_OFFSET_IN_MISC >> 9) #define BOOTLOADER_MESSAGE_BLK_OFFSET (BOOTLOADER_MESSAGE_OFFSET_IN_MISC >> 9)
DECLARE_GLOBAL_DATA_PTR; DECLARE_GLOBAL_DATA_PTR;
struct bootloader_message { struct bootloader_message {
@ -88,26 +86,22 @@ static void boot_lmb_init(bootm_headers_t *images)
#endif #endif
/* /*
* non-OTA packaged kernel.img & boot.img * non-OTA packaged kernel.img & boot.img return the image size on success,
* return the image size on success, and a * and a negative value on error.
* negative value on error.
*/ */
int read_rockchip_image(struct blk_desc *dev_desc, int read_rockchip_image(struct blk_desc *dev_desc,
disk_partition_t *part_info, void *dst) disk_partition_t *part_info, void *dst)
{ {
struct rockchip_image *img; struct rockchip_image *img;
int header_len = 8; int header_len = 8;
int cnt; int cnt, ret;
int ret;
#ifdef CONFIG_ROCKCHIP_CRC #ifdef CONFIG_ROCKCHIP_CRC
u32 crc32; u32 crc32;
#endif #endif
img = memalign(ARCH_DMA_MINALIGN, RK_BLK_SIZE); img = memalign(ARCH_DMA_MINALIGN, RK_BLK_SIZE);
if (!img) { if (!img)
printf("out of memory\n");
return -ENOMEM; return -ENOMEM;
}
/* read first block with header imformation */ /* read first block with header imformation */
ret = blk_dread(dev_desc, part_info->start, 1, img); ret = blk_dread(dev_desc, part_info->start, 1, img);
@ -117,14 +111,15 @@ int read_rockchip_image(struct blk_desc *dev_desc,
} }
if (img->tag != TAG_KERNEL) { if (img->tag != TAG_KERNEL) {
printf("%s: invalid image tag(0x%x)\n", part_info->name, img->tag); printf("Invalid %s image tag(0x%x)\n",
part_info->name, img->tag);
ret = -EINVAL; ret = -EINVAL;
goto err; goto err;
} }
/* /*
* read the rest blks * read the rest blks
* total size = image size + 8 bytes header + 4 bytes crc32 * total size = image size + 8 bytes header + 4 bytes crc32
*/ */
cnt = DIV_ROUND_UP(img->size + 8 + 4, RK_BLK_SIZE); cnt = DIV_ROUND_UP(img->size + 8 + 4, RK_BLK_SIZE);
if (!sysmem_alloc_base_by_name((const char *)part_info->name, if (!sysmem_alloc_base_by_name((const char *)part_info->name,
@ -138,8 +133,7 @@ int read_rockchip_image(struct blk_desc *dev_desc,
ret = blk_dread(dev_desc, part_info->start + 1, cnt - 1, ret = blk_dread(dev_desc, part_info->start + 1, cnt - 1,
dst + RK_BLK_SIZE - header_len); dst + RK_BLK_SIZE - header_len);
if (ret != (cnt - 1)) { if (ret != (cnt - 1)) {
printf("%s try to read %d blocks failed, only read %d blocks\n", printf("Read %s part failed, ret=%d\n", part_info->name, ret);
part_info->name, cnt - 1, ret);
ret = -EIO; ret = -EIO;
} else { } else {
ret = img->size; ret = img->size;
@ -147,8 +141,7 @@ int read_rockchip_image(struct blk_desc *dev_desc,
#ifdef CONFIG_ROCKCHIP_CRC #ifdef CONFIG_ROCKCHIP_CRC
printf("%s image CRC32 verify... ", part_info->name); printf("%s image CRC32 verify... ", part_info->name);
crc32 = rockchip_crc_verify((unsigned char *)(unsigned long)dst, crc32 = rockchip_crc_verify((uchar *)(unsigned long)dst, img->size + 4);
img->size + 4);
if (!crc32) { if (!crc32) {
printf("fail!\n"); printf("fail!\n");
ret = -EINVAL; ret = -EINVAL;
@ -165,11 +158,11 @@ err:
/* Gets the storage type of the current device */ /* Gets the storage type of the current device */
int get_bootdev_type(void) int get_bootdev_type(void)
{ {
int type = 0;
ulong devnum = 0;
char *boot_media = NULL, *devtype = NULL; char *boot_media = NULL, *devtype = NULL;
char boot_options[128] = {0}; char boot_options[128] = {0};
static int appended; static int appended;
ulong devnum = 0;
int type = 0;
devtype = env_get("devtype"); devtype = env_get("devtype");
devnum = env_get_ulong("devnum", 10, 0); devnum = env_get_ulong("devnum", 10, 0);
@ -277,7 +270,7 @@ struct blk_desc *rockchip_get_bootdev(void)
dev_desc = blk_get_devnum_by_type(dev_type, devnum); dev_desc = blk_get_devnum_by_type(dev_type, devnum);
if (!dev_desc) { if (!dev_desc) {
printf("%s: can't find dev_desc!\n", __func__); printf("%s: Can't find dev_desc!\n", __func__);
return NULL; return NULL;
} }
@ -291,7 +284,7 @@ struct blk_desc *rockchip_get_bootdev(void)
mmc = find_mmc_device(devnum); mmc = find_mmc_device(devnum);
printf("MMC%d: %s, %dMhz\n", devnum, printf("MMC%d: %s, %dMhz\n", devnum,
timing[mmc->timing], mmc->clock / 1000000); timing[mmc->timing], mmc->clock / 1000000);
} }
#endif #endif
@ -306,6 +299,7 @@ static void rkloader_set_bootloader_msg(struct bootloader_message *bmsg)
{ {
struct blk_desc *dev_desc; struct blk_desc *dev_desc;
disk_partition_t part_info; disk_partition_t part_info;
int ret, cnt;
dev_desc = rockchip_get_bootdev(); dev_desc = rockchip_get_bootdev();
if (!dev_desc) { if (!dev_desc) {
@ -313,28 +307,23 @@ static void rkloader_set_bootloader_msg(struct bootloader_message *bmsg)
return; return;
} }
int ret = part_get_info_by_name(dev_desc, PART_MISC, ret = part_get_info_by_name(dev_desc, PART_MISC, &part_info);
&part_info);
if (ret < 0) { if (ret < 0) {
printf("not found misc partition.\n"); printf("%s: Could not found misc partition\n", __func__);
return; return;
} }
int size = DIV_ROUND_UP(sizeof(struct bootloader_message), RK_BLK_SIZE)
* RK_BLK_SIZE; cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), dev_desc->blksz);
ret = blk_dwrite(dev_desc, part_info.start + BOOTLOADER_MESSAGE_BLK_OFFSET, ret = blk_dwrite(dev_desc,
size >> 9, bmsg); part_info.start + BOOTLOADER_MESSAGE_BLK_OFFSET,
if (ret != (size >> 9)) { cnt, bmsg);
printf("wape data failed!"); if (ret != cnt)
} printf("%s: Wipe data failed\n", __func__);
} }
void board_run_recovery(void) void board_run_recovery(void)
{ {
char *const boot_recovery_cmd[] = {"run", "boot_recovery_cmd", NULL}; run_command("bootrkp boot-recovery", 0);
env_set("boot_recovery_cmd", "bootrkp boot-recovery");
do_run(NULL, 0, ARRAY_SIZE(boot_recovery_cmd), boot_recovery_cmd);
} }
void board_run_recovery_wipe_data(void) void board_run_recovery_wipe_data(void)
@ -342,6 +331,7 @@ void board_run_recovery_wipe_data(void)
struct bootloader_message bmsg; struct bootloader_message bmsg;
struct blk_desc *dev_desc; struct blk_desc *dev_desc;
disk_partition_t part_info; disk_partition_t part_info;
int ret;
printf("Rebooting into recovery to do wipe_data\n"); printf("Rebooting into recovery to do wipe_data\n");
dev_desc = rockchip_get_bootdev(); dev_desc = rockchip_get_bootdev();
@ -350,20 +340,17 @@ void board_run_recovery_wipe_data(void)
return; return;
} }
int ret; ret = part_get_info_by_name(dev_desc, PART_MISC, &part_info);
ret = part_get_info_by_name(dev_desc, PART_MISC,
&part_info);
if (ret < 0) { if (ret < 0) {
printf("not found misc partition, just run recovery.\n"); printf("%s: Could not found misc partition, just run recovery\n",
__func__);
board_run_recovery(); board_run_recovery();
} }
memset((char *)&bmsg, 0, sizeof(struct bootloader_message)); memset((char *)&bmsg, 0, sizeof(struct bootloader_message));
strcpy(bmsg.command, "boot-recovery"); strcpy(bmsg.command, "boot-recovery");
bmsg.status[0] = 0;
strcpy(bmsg.recovery, "recovery\n--wipe_data"); strcpy(bmsg.recovery, "recovery\n--wipe_data");
bmsg.status[0] = 0;
rkloader_set_bootloader_msg(&bmsg); rkloader_set_bootloader_msg(&bmsg);
@ -385,15 +372,13 @@ void board_run_recovery_wipe_data(void)
*/ */
int rockchip_get_boot_mode(void) int rockchip_get_boot_mode(void)
{ {
struct bootloader_message *bmsg = NULL;
struct blk_desc *dev_desc; struct blk_desc *dev_desc;
disk_partition_t part_info; disk_partition_t part_info;
struct bootloader_message *bmsg = NULL;
int size = DIV_ROUND_UP(sizeof(struct bootloader_message), RK_BLK_SIZE)
* RK_BLK_SIZE;
int ret;
uint32_t reg_boot_mode; uint32_t reg_boot_mode;
char *env_reboot_mode; char *env_reboot_mode;
int clear_boot_reg = 0; int clear_boot_reg = 0;
int ret, cnt;
/* /*
* Here, we mainly check for: * Here, we mainly check for:
@ -419,18 +404,19 @@ int rockchip_get_boot_mode(void)
printf("%s: dev_desc is NULL!\n", __func__); printf("%s: dev_desc is NULL!\n", __func__);
return -ENODEV; return -ENODEV;
} }
ret = part_get_info_by_name(dev_desc, PART_MISC,
&part_info); ret = part_get_info_by_name(dev_desc, PART_MISC, &part_info);
if (ret < 0) { if (ret < 0) {
printf("get part %s fail %d\n", PART_MISC, ret); printf("%s: Could not found misc partition\n", __func__);
goto fallback; goto fallback;
} }
bmsg = memalign(ARCH_DMA_MINALIGN, size); cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), dev_desc->blksz);
bmsg = memalign(ARCH_DMA_MINALIGN, cnt * dev_desc->blksz);
ret = blk_dread(dev_desc, ret = blk_dread(dev_desc,
part_info.start + BOOTLOADER_MESSAGE_BLK_OFFSET, part_info.start + BOOTLOADER_MESSAGE_BLK_OFFSET,
size >> 9, bmsg); cnt, bmsg);
if (ret != (size >> 9)) { if (ret != cnt) {
free(bmsg); free(bmsg);
return -EIO; return -EIO;
} }
@ -523,22 +509,19 @@ static void fdt_ramdisk_skip_relocation(void)
int boot_rockchip_image(struct blk_desc *dev_desc, disk_partition_t *boot_part) int boot_rockchip_image(struct blk_desc *dev_desc, disk_partition_t *boot_part)
{ {
ulong fdt_addr_r = env_get_ulong("fdt_addr_r", 16, 0);
ulong ramdisk_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0); ulong ramdisk_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0);
ulong kernel_addr_r = env_get_ulong("kernel_addr_r", 16, 0); ulong kernel_addr_r = env_get_ulong("kernel_addr_r", 16, 0);
ulong fdt_addr_r = env_get_ulong("fdt_addr_r", 16, 0);
disk_partition_t kernel_part; disk_partition_t kernel_part;
int ramdisk_size; int ramdisk_size;
int kernel_size; int kernel_size;
int fdt_size; int fdt_size;
int ret = 0; int ret = 0;
int part_num;
printf("=Booting Rockchip format image=\n"); printf("=Booting Rockchip format image=\n");
part_num = part_get_info_by_name(dev_desc, PART_KERNEL, ret = part_get_info_by_name(dev_desc, PART_KERNEL, &kernel_part);
&kernel_part); if (ret < 0 || !boot_part) {
printf("Could not found misc partition\n");
if (part_num < 0 || !boot_part) {
printf("%s kernel or boot part info error\n", __func__);
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
} }
@ -546,7 +529,7 @@ int boot_rockchip_image(struct blk_desc *dev_desc, disk_partition_t *boot_part)
kernel_size = read_rockchip_image(dev_desc, &kernel_part, kernel_size = read_rockchip_image(dev_desc, &kernel_part,
(void *)kernel_addr_r); (void *)kernel_addr_r);
if (kernel_size < 0) { if (kernel_size < 0) {
printf("%s kernel part read error\n", __func__); printf("Read kernel image failed, ret=%d\n", ret);
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
} }
@ -554,8 +537,7 @@ int boot_rockchip_image(struct blk_desc *dev_desc, disk_partition_t *boot_part)
ramdisk_size = read_rockchip_image(dev_desc, boot_part, ramdisk_size = read_rockchip_image(dev_desc, boot_part,
(void *)ramdisk_addr_r); (void *)ramdisk_addr_r);
if (ramdisk_size < 0) { if (ramdisk_size < 0) {
printf("%s ramdisk part %s read error\n", __func__, printf("Read ramdisk image failed, ret=%d\n", ret);
boot_part->name);
ramdisk_size = 0; ramdisk_size = 0;
} }
@ -571,7 +553,7 @@ int boot_rockchip_image(struct blk_desc *dev_desc, disk_partition_t *boot_part)
if (gd->fdt_blob != (void *)fdt_addr_r) { if (gd->fdt_blob != (void *)fdt_addr_r) {
fdt_size = rockchip_read_dtb_file((void *)fdt_addr_r); fdt_size = rockchip_read_dtb_file((void *)fdt_addr_r);
if (fdt_size < 0) { if (fdt_size < 0) {
printf("%s fdt read error\n", __func__); printf("Read fdt failed, ret=%d\n", fdt_size);
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
} }
@ -589,8 +571,9 @@ int boot_rockchip_image(struct blk_desc *dev_desc, disk_partition_t *boot_part)
#if defined(CONFIG_ARM64) #if defined(CONFIG_ARM64)
char cmdbuf[64]; char cmdbuf[64];
sprintf(cmdbuf, "booti 0x%lx 0x%lx:0x%x 0x%lx",
kernel_addr_r, ramdisk_addr_r, ramdisk_size, fdt_addr_r); snprintf(cmdbuf, 64, "booti 0x%lx 0x%lx:0x%x 0x%lx",
kernel_addr_r, ramdisk_addr_r, ramdisk_size, fdt_addr_r);
run_command(cmdbuf, 0); run_command(cmdbuf, 0);
#else #else
/* We asume it's always zImage on 32-bit platform */ /* We asume it's always zImage on 32-bit platform */