summaryrefslogtreecommitdiff
path: root/board/ti/common/k3_dfu.c
blob: 595459094402261e5516e923e4e263891507cfe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2023, Texas Instruments Incorporated - https://www.ti.com/
 */

#include <common.h>
#include <blk.h>
#include <dm.h>
#include <dfu.h>
#include <env.h>
#include <memalign.h>
#include <misc.h>
#include <mtd.h>
#include <mtd_node.h>

#define DFU_ALT_BUF_LEN 512

static void board_get_alt_info_sf(struct mtd_info *mtd, char *buf)
{
	struct mtd_info *part;
	bool first = true;
	const char *name;
	int len;

	name = mtd->name;
	len = strlen(buf);

	list_for_each_entry(part, &mtd->partitions, node) {
		if (!first)
			len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, "; ");
		first = false;
		len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
				"%s raw %llx %llx", part->name, part->offset, part->size);
	}
}

void set_dfu_alt_info(char *interface, char *devstr)
{
	struct udevice *dev;
	struct mtd_info *mtd;
	char *st, *devstr_bkup;
	unsigned int bus;

	ALLOC_CACHE_ALIGN_BUFFER(char, buf, DFU_ALT_BUF_LEN);

	if (env_get("dfu_alt_info"))
		return;

	memset(buf, 0, sizeof(buf));

	if (!strcmp(interface, "sf")) {
		devstr_bkup = strdup(devstr);
		st = strsep(&devstr_bkup, ":");
		if (!st || !*st) {
			printf("Invalid SPI bus %s\n", st);
			return;
		}
		bus = simple_strtoul(st, NULL, 0);

		if (uclass_get_device(UCLASS_SPI_FLASH, bus, &dev)) {
			printf("Failed to get device on bus %d\n", bus);
			return;
		}
		if (CONFIG_IS_ENABLED(MTD)) {
			mtd_probe_devices();
			mtd_for_each_device(mtd) {
				if (!mtd_is_partition(mtd) && mtd->dev && dev == mtd->dev)
					board_get_alt_info_sf(mtd, buf);
			}
		}
	} else {
		printf("dynamic dfu_alt_info supported only for sf\n");
		return;
	}

	env_set("dfu_alt_info", buf);
}