Files
luban-lite-t3e-pro/application/baremetal/bootloader/cmd/mmc_boot_no_fitimage.c

110 lines
2.4 KiB
C
Raw Normal View History

2023-12-12 14:23:53 +08:00
/*
2025-01-08 19:12:06 +08:00
* Copyright (c) 2023-2024, ArtInChip Technology Co., Ltd
2023-12-12 14:23:53 +08:00
*
* SPDX-License-Identifier: Apache-2.0
*
* Wu Dehuang <dehuang.wu@artinchip.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <console.h>
#include <aic_common.h>
#include <aic_errno.h>
2024-01-27 08:47:24 +08:00
#include <boot_param.h>
2023-12-12 14:23:53 +08:00
#include <mmc.h>
#include <image.h>
#include <boot.h>
2024-04-03 16:40:57 +08:00
#include <aic_utils.h>
2023-12-12 14:23:53 +08:00
#define APPLICATION_PART "os"
static int do_mmc_boot(int argc, char *argv[])
{
2024-01-27 08:47:24 +08:00
int ret = 0, mmc_id = 0;
enum boot_device bd;
2023-12-12 14:23:53 +08:00
struct image_header *head = NULL;
struct aic_sdmc *host = NULL;
struct aic_partition *part = NULL, *parts = NULL;
void *la;
u64 blkstart, blkcnt;
u32 start_us;
2024-01-27 08:47:24 +08:00
bd = aic_get_boot_device();
if (BD_SDMC0 == bd) {
mmc_id = 0;
} else if (BD_SDMC1 == bd) {
mmc_id = 1;
}
2023-12-12 14:23:53 +08:00
ret = mmc_init(mmc_id);
if (ret) {
printf("sdmc %d init failed.\n", mmc_id);
return ret;
}
host = find_mmc_dev_by_index(mmc_id);
if (!host) {
pr_err("find mmc dev failed.\n");
return -1;
}
2025-01-08 19:12:06 +08:00
parts = mmc_create_gpt_part2(mmc_id);
2023-12-12 14:23:53 +08:00
if (!parts) {
pr_err("sdmc %d create gpt part failed.\n", mmc_id);
goto out;
}
part = parts;
while (part) {
if (!strcmp(part->name, APPLICATION_PART))
break;
part = part->next;
}
if (!part) {
printf("Failed to get application partition.\n");
goto out;
}
head = malloc(MMC_BLOCK_SIZE);
blkstart = part->start / MMC_BLOCK_SIZE;
ret = mmc_bread(host, blkstart, 1, (void *)head);
if (ret < 0) {
printf("Read image header failed.\n");
goto out;
}
ret = image_verify_magic((void *)head, AIC_IMAGE_MAGIC);
if (ret) {
printf("Application header is unknown.\n");
goto out;
}
la = (void *)(unsigned long)head->load_address;
start_us = aic_get_time_us();
blkcnt = ROUNDUP(head->image_len, MMC_BLOCK_SIZE) / MMC_BLOCK_SIZE;
ret = mmc_bread(host, blkstart, blkcnt, la);
show_speed("mmc read speed", head->image_len, aic_get_time_us() - start_us);
if (ret < 0) {
printf("Read image failed.\n");
goto out;
}
boot_app(la);
out:
if (parts)
mmc_free_partition(parts);
if (head)
free(head);
return ret;
}
CONSOLE_CMD(mmc_boot, do_mmc_boot, "Boot from eMMC/SD.");