blob: 77edb86e78c1361e4f161f3ead263a5d7a490f58 [file] [log] [blame]
Patrick Delaunaycf073432020-03-18 09:22:45 +01001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * Copyright (C) 2020, STMicroelectronics - All Rights Reserved
4 */
5
6#include <common.h>
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +01007#include <blk.h>
Simon Glass7130b952020-07-19 10:15:40 -06008#include <dm.h>
Patrick Delaunaycf073432020-03-18 09:22:45 +01009#include <dfu.h>
10#include <env.h>
Patrick Delaunay9742bee2020-11-06 19:02:00 +010011#include <log.h>
Patrick Delaunaycf073432020-03-18 09:22:45 +010012#include <memalign.h>
13#include <misc.h>
14#include <mtd.h>
15#include <mtd_node.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010016#include <asm/arch/stm32prog.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060017#include <linux/printk.h>
Patrick Delaunaycf073432020-03-18 09:22:45 +010018
19#define DFU_ALT_BUF_LEN SZ_1K
20
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010021static void board_get_alt_info_mmc(struct udevice *dev, char *buf)
Patrick Delaunaycf073432020-03-18 09:22:45 +010022{
Simon Glassc1c4a8f2020-05-10 11:39:57 -060023 struct disk_partition info;
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010024 int p, len, devnum;
25 bool first = true;
26 const char *name;
27 struct mmc *mmc;
28 struct blk_desc *desc;
Patrick Delaunaycf073432020-03-18 09:22:45 +010029
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010030 mmc = mmc_get_mmc_dev(dev);
31 if (!mmc)
32 return;
33
34 if (mmc_init(mmc))
35 return;
36
37 desc = mmc_get_blk_desc(mmc);
38 if (!desc)
39 return;
40
Simon Glassfada3f92022-09-17 09:00:09 -060041 name = blk_get_uclass_name(desc->uclass_id);
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010042 devnum = desc->devnum;
43 len = strlen(buf);
Patrick Delaunaycf073432020-03-18 09:22:45 +010044
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010045 if (buf[0] != '\0')
46 len += snprintf(buf + len,
47 DFU_ALT_BUF_LEN - len, "&");
48 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
49 "%s %d=", name, devnum);
50
51 if (IS_MMC(mmc) && mmc->capacity_boot) {
52 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
53 "%s%d_boot1 raw 0x0 0x%llx mmcpart 1;",
54 name, devnum, mmc->capacity_boot);
55 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
56 "%s%d_boot2 raw 0x0 0x%llx mmcpart 2",
57 name, devnum, mmc->capacity_boot);
58 first = false;
59 }
60
Heinrich Schuchardt890c55c2022-01-11 15:58:08 +010061 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010062 if (part_get_info(desc, p, &info))
63 continue;
64 if (!first)
65 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, ";");
66 first = false;
67 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
68 "%s%d_%s part %d %d",
69 name, devnum, info.name, devnum, p);
Patrick Delaunaycf073432020-03-18 09:22:45 +010070 }
71}
72
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010073static void board_get_alt_info_mtd(struct mtd_info *mtd, char *buf)
74{
75 struct mtd_info *part;
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010076 const char *name;
77 int len, partnum = 0;
78
79 name = mtd->name;
80 len = strlen(buf);
81
82 if (buf[0] != '\0')
83 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, "&");
84 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
85 "mtd %s=", name);
86
87 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
Patrice Chotard2ef12a82023-11-17 18:01:06 +010088 "%s raw 0x0 0x%llx",
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010089 name, mtd->size);
90
91 list_for_each_entry(part, &mtd->partitions, node) {
92 partnum++;
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010093 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
Patrice Chotard2ef12a82023-11-17 18:01:06 +010094 ";%s_%s part %d",
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010095 name, part->name, partnum);
96 }
97}
98
Patrick Delaunaycf073432020-03-18 09:22:45 +010099void set_dfu_alt_info(char *interface, char *devstr)
100{
101 struct udevice *dev;
102 struct mtd_info *mtd;
103
104 ALLOC_CACHE_ALIGN_BUFFER(char, buf, DFU_ALT_BUF_LEN);
105
106 if (env_get("dfu_alt_info"))
107 return;
108
109 memset(buf, 0, sizeof(buf));
110
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +0100111 snprintf(buf, DFU_ALT_BUF_LEN,
112 "ram 0=%s", CONFIG_DFU_ALT_RAM0);
Patrick Delaunaycf073432020-03-18 09:22:45 +0100113
Patrick Delaunayb7b67522020-07-31 16:31:46 +0200114 if (CONFIG_IS_ENABLED(MMC)) {
115 if (!uclass_get_device(UCLASS_MMC, 0, &dev))
116 board_get_alt_info_mmc(dev, buf);
Patrick Delaunaycf073432020-03-18 09:22:45 +0100117
Patrick Delaunayb7b67522020-07-31 16:31:46 +0200118 if (!uclass_get_device(UCLASS_MMC, 1, &dev))
119 board_get_alt_info_mmc(dev, buf);
120 }
Patrick Delaunaycf073432020-03-18 09:22:45 +0100121
Simon Glass8e05bb12023-02-05 15:40:17 -0700122 if (IS_ENABLED(CONFIG_MTD)) {
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +0100123 /* probe all MTD devices */
124 mtd_probe_devices();
Patrick Delaunaycf073432020-03-18 09:22:45 +0100125
Patrice Chotard16434e12023-11-17 18:01:07 +0100126 mtd_for_each_device(mtd)
127 if (!mtd_is_partition(mtd))
Patrick Delaunayb82c97e2021-11-25 11:54:53 +0100128 board_get_alt_info_mtd(mtd, buf);
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +0100129 }
Patrick Delaunaycf073432020-03-18 09:22:45 +0100130
Patrick Delaunay16d37712023-09-26 17:09:23 +0200131 if (IS_ENABLED(CONFIG_DFU_VIRT)) {
132 /* virtual device id 0 is aligned with stm32mp_dfu_virt.c */
133 strlcat(buf, "&virt 0=OTP", DFU_ALT_BUF_LEN);
Patrick Delaunaycf073432020-03-18 09:22:45 +0100134
Patrick Delaunayb7b67522020-07-31 16:31:46 +0200135 if (IS_ENABLED(CONFIG_PMIC_STPMIC1))
Patrick Delaunay16d37712023-09-26 17:09:23 +0200136 strlcat(buf, "&virt 1=PMIC", DFU_ALT_BUF_LEN);
Patrick Delaunayb7b67522020-07-31 16:31:46 +0200137 }
Patrick Delaunaycf073432020-03-18 09:22:45 +0100138
139 env_set("dfu_alt_info", buf);
140 puts("DFU alt info setting: done\n");
141}