X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=cmd%2Fmmc.c;h=97e5d91859c9fc9e2ec200fcc128c4c0d1a4b5a6;hb=09140113108541b95d340f3c7b6ee597d31ccc73;hp=5def4ea1a29ad0b6c72bd60b3c4132c749a1d6ca;hpb=2dc5b553b9bcd701bd7abd60f99f407cb0c37762;p=oweals%2Fu-boot.git diff --git a/cmd/mmc.c b/cmd/mmc.c index 5def4ea1a2..97e5d91859 100644 --- a/cmd/mmc.c +++ b/cmd/mmc.c @@ -1,14 +1,17 @@ +// SPDX-License-Identifier: GPL-2.0+ /* * (C) Copyright 2003 * Kyle Harris, kharris@nexus-tech.net - * - * SPDX-License-Identifier: GPL-2.0+ */ #include +#include #include #include #include +#include +#include +#include static int curr_device = -1; @@ -23,7 +26,12 @@ static void print_mmcinfo(struct mmc *mmc) (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff); - printf("Tran Speed: %d\n", mmc->tran_speed); + printf("Bus Speed: %d\n", mmc->clock); +#if CONFIG_IS_ENABLED(MMC_VERBOSE) + printf("Mode: %s\n", mmc_mode_name(mmc->selected_mode)); + mmc_dump_capabilities("card capabilities", mmc->card_caps); + mmc_dump_capabilities("host capabilities", mmc->host_caps); +#endif printf("Rd Block Len: %d\n", mmc->read_bl_len); printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC", @@ -40,15 +48,21 @@ static void print_mmcinfo(struct mmc *mmc) printf("Bus Width: %d-bit%s\n", mmc->bus_width, mmc->ddr_mode ? " DDR" : ""); +#if CONFIG_IS_ENABLED(MMC_WRITE) puts("Erase Group Size: "); print_size(((u64)mmc->erase_grp_size) << 9, "\n"); +#endif if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) { bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0; bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR); + u8 wp, ext_csd[MMC_MAX_BLOCK_LEN]; + int ret; +#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING) puts("HC WP Group Size: "); print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n"); +#endif puts("User Capacity: "); print_size(mmc->capacity_user, usr_enh ? " ENH" : ""); @@ -80,6 +94,28 @@ static void print_mmcinfo(struct mmc *mmc) putc('\n'); } } + ret = mmc_send_ext_csd(mmc, ext_csd); + if (ret) + return; + wp = ext_csd[EXT_CSD_BOOT_WP_STATUS]; + for (i = 0; i < 2; ++i) { + printf("Boot area %d is ", i); + switch (wp & 3) { + case 0: + printf("not write protected\n"); + break; + case 1: + printf("power on protected\n"); + break; + case 2: + printf("permanently protected\n"); + break; + default: + printf("in reserved protection state\n"); + break; + } + wp >>= 2; + } } } static struct mmc *init_mmc_device(int dev, bool force_init) @@ -91,13 +127,24 @@ static struct mmc *init_mmc_device(int dev, bool force_init) return NULL; } + if (!mmc_getcd(mmc)) + force_init = true; + if (force_init) mmc->has_init = 0; if (mmc_init(mmc)) return NULL; + +#ifdef CONFIG_BLOCK_CACHE + struct blk_desc *bd = mmc_get_blk_desc(mmc); + blkcache_invalidate(bd->if_type, bd->devnum); +#endif + return mmc; } -static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) + +static int do_mmcinfo(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) { struct mmc *mmc; @@ -118,7 +165,7 @@ static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) return CMD_RET_SUCCESS; } -#ifdef CONFIG_SUPPORT_EMMC_RPMB +#if CONFIG_IS_ENABLED(CMD_MMC_RPMB) static int confirm_key_prog(void) { puts("Warning: Programming authentication key can be done only once !\n" @@ -130,8 +177,9 @@ static int confirm_key_prog(void) puts("Authentication key programming aborted\n"); return 0; } -static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag, - int argc, char * const argv[]) + +static int do_mmcrpmb_key(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) { void *key_addr; struct mmc *mmc = find_mmc_device(curr_device); @@ -148,8 +196,9 @@ static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag, } return CMD_RET_SUCCESS; } -static int do_mmcrpmb_read(cmd_tbl_t *cmdtp, int flag, - int argc, char * const argv[]) + +static int do_mmcrpmb_read(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) { u16 blk, cnt; void *addr; @@ -176,8 +225,9 @@ static int do_mmcrpmb_read(cmd_tbl_t *cmdtp, int flag, return CMD_RET_FAILURE; return CMD_RET_SUCCESS; } -static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag, - int argc, char * const argv[]) + +static int do_mmcrpmb_write(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) { u16 blk, cnt; void *addr; @@ -202,8 +252,9 @@ static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag, return CMD_RET_FAILURE; return CMD_RET_SUCCESS; } -static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag, - int argc, char * const argv[]) + +static int do_mmcrpmb_counter(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) { unsigned long counter; struct mmc *mmc = find_mmc_device(curr_device); @@ -214,17 +265,17 @@ static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag, return CMD_RET_SUCCESS; } -static cmd_tbl_t cmd_rpmb[] = { +static struct cmd_tbl cmd_rpmb[] = { U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""), U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""), U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""), U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""), }; -static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag, - int argc, char * const argv[]) +static int do_mmcrpmb(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) { - cmd_tbl_t *cp; + struct cmd_tbl *cp; struct mmc *mmc; char original_part; int ret; @@ -237,7 +288,7 @@ static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag, if (cp == NULL || argc > cp->maxargs) return CMD_RET_USAGE; - if (flag == CMD_FLAG_REPEAT && !cp->repeatable) + if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp)) return CMD_RET_SUCCESS; mmc = init_mmc_device(curr_device, false); @@ -245,7 +296,7 @@ static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag, return CMD_RET_FAILURE; if (!(mmc->version & MMC_VERSION_MMC)) { - printf("It is not a EMMC device\n"); + printf("It is not an eMMC device\n"); return CMD_RET_FAILURE; } if (mmc->version < MMC_VERSION_4_41) { @@ -271,8 +322,8 @@ static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag, } #endif -static int do_mmc_read(cmd_tbl_t *cmdtp, int flag, - int argc, char * const argv[]) +static int do_mmc_read(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) { struct mmc *mmc; u32 blk, cnt, n; @@ -297,8 +348,75 @@ static int do_mmc_read(cmd_tbl_t *cmdtp, int flag, return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE; } -static int do_mmc_write(cmd_tbl_t *cmdtp, int flag, - int argc, char * const argv[]) + +#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE) +static lbaint_t mmc_sparse_write(struct sparse_storage *info, lbaint_t blk, + lbaint_t blkcnt, const void *buffer) +{ + struct blk_desc *dev_desc = info->priv; + + return blk_dwrite(dev_desc, blk, blkcnt, buffer); +} + +static lbaint_t mmc_sparse_reserve(struct sparse_storage *info, + lbaint_t blk, lbaint_t blkcnt) +{ + return blkcnt; +} + +static int do_mmc_sparse_write(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) +{ + struct sparse_storage sparse; + struct blk_desc *dev_desc; + struct mmc *mmc; + char dest[11]; + void *addr; + u32 blk; + + if (argc != 3) + return CMD_RET_USAGE; + + addr = (void *)simple_strtoul(argv[1], NULL, 16); + blk = simple_strtoul(argv[2], NULL, 16); + + if (!is_sparse_image(addr)) { + printf("Not a sparse image\n"); + return CMD_RET_FAILURE; + } + + mmc = init_mmc_device(curr_device, false); + if (!mmc) + return CMD_RET_FAILURE; + + printf("\nMMC Sparse write: dev # %d, block # %d ... ", + curr_device, blk); + + if (mmc_getwp(mmc) == 1) { + printf("Error: card is write protected!\n"); + return CMD_RET_FAILURE; + } + + dev_desc = mmc_get_blk_desc(mmc); + sparse.priv = dev_desc; + sparse.blksz = 512; + sparse.start = blk; + sparse.size = dev_desc->lba - blk; + sparse.write = mmc_sparse_write; + sparse.reserve = mmc_sparse_reserve; + sparse.mssg = NULL; + sprintf(dest, "0x" LBAF, sparse.start * sparse.blksz); + + if (write_sparse_image(&sparse, dest, addr, NULL)) + return CMD_RET_FAILURE; + else + return CMD_RET_SUCCESS; +} +#endif + +#if CONFIG_IS_ENABLED(MMC_WRITE) +static int do_mmc_write(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) { struct mmc *mmc; u32 blk, cnt, n; @@ -327,8 +445,9 @@ static int do_mmc_write(cmd_tbl_t *cmdtp, int flag, return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE; } -static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag, - int argc, char * const argv[]) + +static int do_mmc_erase(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) { struct mmc *mmc; u32 blk, cnt, n; @@ -355,8 +474,10 @@ static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag, return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE; } -static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag, - int argc, char * const argv[]) +#endif + +static int do_mmc_rescan(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) { struct mmc *mmc; @@ -366,8 +487,9 @@ static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag, return CMD_RET_SUCCESS; } -static int do_mmc_part(cmd_tbl_t *cmdtp, int flag, - int argc, char * const argv[]) + +static int do_mmc_part(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) { struct blk_desc *mmc_dev; struct mmc *mmc; @@ -385,8 +507,9 @@ static int do_mmc_part(cmd_tbl_t *cmdtp, int flag, puts("get mmc type error!\n"); return CMD_RET_FAILURE; } -static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag, - int argc, char * const argv[]) + +static int do_mmc_dev(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) { int dev, part = 0, ret; struct mmc *mmc; @@ -426,15 +549,17 @@ static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag, return CMD_RET_SUCCESS; } -static int do_mmc_list(cmd_tbl_t *cmdtp, int flag, - int argc, char * const argv[]) + +static int do_mmc_list(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) { print_mmc_devices('\n'); return CMD_RET_SUCCESS; } +#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING) static int parse_hwpart_user(struct mmc_hwpart_conf *pconf, - int argc, char * const argv[]) + int argc, char *const argv[]) { int i = 0; @@ -468,7 +593,7 @@ static int parse_hwpart_user(struct mmc_hwpart_conf *pconf, } static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx, - int argc, char * const argv[]) + int argc, char *const argv[]) { int i; @@ -501,8 +626,8 @@ static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx, return i; } -static int do_mmc_hwpartition(cmd_tbl_t *cmdtp, int flag, - int argc, char * const argv[]) +static int do_mmc_hwpartition(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) { struct mmc *mmc; struct mmc_hwpart_conf pconf = { }; @@ -582,10 +707,11 @@ static int do_mmc_hwpartition(cmd_tbl_t *cmdtp, int flag, return CMD_RET_FAILURE; } } +#endif #ifdef CONFIG_SUPPORT_EMMC_BOOT -static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag, - int argc, char * const argv[]) +static int do_mmc_bootbus(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) { int dev; struct mmc *mmc; @@ -610,8 +736,9 @@ static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag, /* acknowledge to be sent during boot operation */ return mmc_set_boot_bus_width(mmc, width, reset, mode); } -static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag, - int argc, char * const argv[]) + +static int do_mmc_boot_resize(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) { int dev; struct mmc *mmc; @@ -628,7 +755,7 @@ static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag, return CMD_RET_FAILURE; if (IS_SD(mmc)) { - printf("It is not a EMMC device\n"); + printf("It is not an eMMC device\n"); return CMD_RET_FAILURE; } @@ -663,8 +790,8 @@ static int mmc_partconf_print(struct mmc *mmc) return CMD_RET_SUCCESS; } -static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag, - int argc, char * const argv[]) +static int do_mmc_partconf(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) { int dev; struct mmc *mmc; @@ -694,8 +821,9 @@ static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag, /* acknowledge to be sent during boot operation */ return mmc_set_part_conf(mmc, ack, part_num, access); } -static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag, - int argc, char * const argv[]) + +static int do_mmc_rst_func(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) { int dev; struct mmc *mmc; @@ -729,8 +857,8 @@ static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag, return mmc_set_rst_n_function(mmc, enable); } #endif -static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag, - int argc, char * const argv[]) +static int do_mmc_setdsr(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) { struct mmc *mmc; u32 val; @@ -758,8 +886,8 @@ static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag, } #ifdef CONFIG_CMD_BKOPS_ENABLE -static int do_mmc_bkops_enable(cmd_tbl_t *cmdtp, int flag, - int argc, char * const argv[]) +static int do_mmc_bkops_enable(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) { int dev; struct mmc *mmc; @@ -782,23 +910,51 @@ static int do_mmc_bkops_enable(cmd_tbl_t *cmdtp, int flag, } #endif -static cmd_tbl_t cmd_mmc[] = { +static int do_mmc_boot_wp(struct cmd_tbl *cmdtp, int flag, + int argc, char * const argv[]) +{ + int err; + struct mmc *mmc; + + mmc = init_mmc_device(curr_device, false); + if (!mmc) + return CMD_RET_FAILURE; + if (IS_SD(mmc)) { + printf("It is not an eMMC device\n"); + return CMD_RET_FAILURE; + } + err = mmc_boot_wp(mmc); + if (err) + return CMD_RET_FAILURE; + printf("boot areas protected\n"); + return CMD_RET_SUCCESS; +} + +static struct cmd_tbl cmd_mmc[] = { U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""), U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""), + U_BOOT_CMD_MKENT(wp, 1, 0, do_mmc_boot_wp, "", ""), +#if CONFIG_IS_ENABLED(MMC_WRITE) U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""), U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""), +#endif +#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE) + U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""), +#endif U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""), U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""), U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""), U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""), +#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING) U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""), +#endif #ifdef CONFIG_SUPPORT_EMMC_BOOT U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""), U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""), U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""), U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""), #endif -#ifdef CONFIG_SUPPORT_EMMC_RPMB +#if CONFIG_IS_ENABLED(CMD_MMC_RPMB) U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""), #endif U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""), @@ -807,9 +963,10 @@ static cmd_tbl_t cmd_mmc[] = { #endif }; -static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +static int do_mmcops(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) { - cmd_tbl_t *cp; + struct cmd_tbl *cp; cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc)); @@ -819,7 +976,7 @@ static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) if (cp == NULL || argc > cp->maxargs) return CMD_RET_USAGE; - if (flag == CMD_FLAG_REPEAT && !cp->repeatable) + if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp)) return CMD_RET_SUCCESS; if (curr_device < 0) { @@ -839,11 +996,16 @@ U_BOOT_CMD( "info - display info of the current MMC device\n" "mmc read addr blk# cnt\n" "mmc write addr blk# cnt\n" +#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE) + "mmc swrite addr blk#\n" +#endif "mmc erase blk# cnt\n" "mmc rescan\n" "mmc part - lists available partition on current mmc device\n" "mmc dev [dev] [part] - show or set current mmc device [partition]\n" "mmc list - lists available devices\n" + "mmc wp - power on write protect booot partitions\n" +#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING) "mmc hwpartition [args...] - does hardware partitioning\n" " arguments (sizes in 512-byte blocks):\n" " [user [enh start cnt] [wrrel {on|off}]] - sets user data area attributes\n" @@ -851,6 +1013,7 @@ U_BOOT_CMD( " [check|set|complete] - mode, complete set partitioning completed\n" " WARNING: Partitioning is a write-once setting once it is set to complete.\n" " Power cycling is required to initialize partitions after set to complete.\n" +#endif #ifdef CONFIG_SUPPORT_EMMC_BOOT "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n" " - Set the BOOT_BUS_WIDTH field of the specified device\n" @@ -862,7 +1025,7 @@ U_BOOT_CMD( " - Change the RST_n_FUNCTION field of the specified device\n" " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n" #endif -#ifdef CONFIG_SUPPORT_EMMC_RPMB +#if CONFIG_IS_ENABLED(CMD_MMC_RPMB) "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n" "mmc rpmb write addr blk# cnt
- block size is 256 bytes\n" "mmc rpmb key
- program the RPMB authentication key.\n"