blob: 1db8e45480e16874be49d32e3bfc1b856439a6fc [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
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +01006#include <blk.h>
Simon Glass7130b952020-07-19 10:15:40 -06007#include <dm.h>
Patrick Delaunaycf073432020-03-18 09:22:45 +01008#include <dfu.h>
9#include <env.h>
Patrick Delaunay9742bee2020-11-06 19:02:00 +010010#include <log.h>
Patrick Delaunaycf073432020-03-18 09:22:45 +010011#include <memalign.h>
12#include <misc.h>
13#include <mtd.h>
14#include <mtd_node.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010015#include <asm/arch/stm32prog.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060016#include <linux/printk.h>
Patrick Delaunaycf073432020-03-18 09:22:45 +010017
18#define DFU_ALT_BUF_LEN SZ_1K
19
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010020static void board_get_alt_info_mmc(struct udevice *dev, char *buf)
Patrick Delaunaycf073432020-03-18 09:22:45 +010021{
Simon Glassc1c4a8f2020-05-10 11:39:57 -060022 struct disk_partition info;
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010023 int p, len, devnum;
24 bool first = true;
25 const char *name;
26 struct mmc *mmc;
27 struct blk_desc *desc;
Patrick Delaunaycf073432020-03-18 09:22:45 +010028
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010029 mmc = mmc_get_mmc_dev(dev);
30 if (!mmc)
31 return;
32
33 if (mmc_init(mmc))
34 return;
35
36 desc = mmc_get_blk_desc(mmc);
37 if (!desc)
38 return;
39
Simon Glassfada3f92022-09-17 09:00:09 -060040 name = blk_get_uclass_name(desc->uclass_id);
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010041 devnum = desc->devnum;
42 len = strlen(buf);
Patrick Delaunaycf073432020-03-18 09:22:45 +010043
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010044 if (buf[0] != '\0')
45 len += snprintf(buf + len,
46 DFU_ALT_BUF_LEN - len, "&");
47 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
48 "%s %d=", name, devnum);
49
50 if (IS_MMC(mmc) && mmc->capacity_boot) {
51 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
52 "%s%d_boot1 raw 0x0 0x%llx mmcpart 1;",
53 name, devnum, mmc->capacity_boot);
54 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
55 "%s%d_boot2 raw 0x0 0x%llx mmcpart 2",
56 name, devnum, mmc->capacity_boot);
57 first = false;
58 }
59
Heinrich Schuchardt890c55c2022-01-11 15:58:08 +010060 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010061 if (part_get_info(desc, p, &info))
62 continue;
63 if (!first)
64 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, ";");
65 first = false;
66 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
67 "%s%d_%s part %d %d",
68 name, devnum, info.name, devnum, p);
Patrick Delaunaycf073432020-03-18 09:22:45 +010069 }
70}
71
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010072static void board_get_alt_info_mtd(struct mtd_info *mtd, char *buf)
73{
74 struct mtd_info *part;
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010075 const char *name;
76 int len, partnum = 0;
77
78 name = mtd->name;
79 len = strlen(buf);
80
81 if (buf[0] != '\0')
82 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, "&");
83 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
84 "mtd %s=", name);
85
86 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
Patrice Chotard2ef12a82023-11-17 18:01:06 +010087 "%s raw 0x0 0x%llx",
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010088 name, mtd->size);
89
90 list_for_each_entry(part, &mtd->partitions, node) {
91 partnum++;
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010092 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
Patrice Chotard2ef12a82023-11-17 18:01:06 +010093 ";%s_%s part %d",
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +010094 name, part->name, partnum);
95 }
96}
97
Patrick Delaunaycf073432020-03-18 09:22:45 +010098void set_dfu_alt_info(char *interface, char *devstr)
99{
100 struct udevice *dev;
101 struct mtd_info *mtd;
102
103 ALLOC_CACHE_ALIGN_BUFFER(char, buf, DFU_ALT_BUF_LEN);
104
105 if (env_get("dfu_alt_info"))
106 return;
107
108 memset(buf, 0, sizeof(buf));
109
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +0100110 snprintf(buf, DFU_ALT_BUF_LEN,
111 "ram 0=%s", CONFIG_DFU_ALT_RAM0);
Patrick Delaunaycf073432020-03-18 09:22:45 +0100112
Patrick Delaunayb7b67522020-07-31 16:31:46 +0200113 if (CONFIG_IS_ENABLED(MMC)) {
114 if (!uclass_get_device(UCLASS_MMC, 0, &dev))
115 board_get_alt_info_mmc(dev, buf);
Patrick Delaunaycf073432020-03-18 09:22:45 +0100116
Patrick Delaunayb7b67522020-07-31 16:31:46 +0200117 if (!uclass_get_device(UCLASS_MMC, 1, &dev))
118 board_get_alt_info_mmc(dev, buf);
119 }
Patrick Delaunaycf073432020-03-18 09:22:45 +0100120
Simon Glass8e05bb12023-02-05 15:40:17 -0700121 if (IS_ENABLED(CONFIG_MTD)) {
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +0100122 /* probe all MTD devices */
123 mtd_probe_devices();
Patrick Delaunaycf073432020-03-18 09:22:45 +0100124
Patrice Chotard16434e12023-11-17 18:01:07 +0100125 mtd_for_each_device(mtd)
126 if (!mtd_is_partition(mtd))
Patrick Delaunayb82c97e2021-11-25 11:54:53 +0100127 board_get_alt_info_mtd(mtd, buf);
Patrick Delaunayc7d5b8c2020-03-18 09:22:46 +0100128 }
Patrick Delaunaycf073432020-03-18 09:22:45 +0100129
Patrick Delaunay16d37712023-09-26 17:09:23 +0200130 if (IS_ENABLED(CONFIG_DFU_VIRT)) {
131 /* virtual device id 0 is aligned with stm32mp_dfu_virt.c */
132 strlcat(buf, "&virt 0=OTP", DFU_ALT_BUF_LEN);
Patrick Delaunaycf073432020-03-18 09:22:45 +0100133
Patrick Delaunayb7b67522020-07-31 16:31:46 +0200134 if (IS_ENABLED(CONFIG_PMIC_STPMIC1))
Patrick Delaunay16d37712023-09-26 17:09:23 +0200135 strlcat(buf, "&virt 1=PMIC", DFU_ALT_BUF_LEN);
Patrick Delaunayb7b67522020-07-31 16:31:46 +0200136 }
Patrick Delaunaycf073432020-03-18 09:22:45 +0100137
138 env_set("dfu_alt_info", buf);
139 puts("DFU alt info setting: done\n");
140}