summaryrefslogtreecommitdiff
path: root/drivers/misc/fs_loader.c
blob: baa5f8302aeb8c050cbbdacf819fc3e02e095e8d (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// SPDX-License-Identifier: GPL-2.0
 /*
 * Copyright (C) 2018 Intel Corporation <www.intel.com>
 *
 */
#include <common.h>
#include <dm.h>
#include <errno.h>
#include <blk.h>
#include <fs.h>
#include <fs_loader.h>
#include <linux/string.h>
#include <mapmem.h>
#include <malloc.h>
#include <spl.h>

DECLARE_GLOBAL_DATA_PTR;

struct firmware_priv {
	const char *name;	/* Filename */
	u32 offset;		/* Offset of reading a file */
};

#ifdef CONFIG_CMD_UBIFS
static int mount_ubifs(char *mtdpart, char *ubivol)
{
	int ret = ubi_part(mtdpart, NULL);

	if (ret) {
		debug("Cannot find mtd partition %s\n", mtdpart);
		return ret;
	}

	return cmd_ubifs_mount(ubivol);
}

static int umount_ubifs(void)
{
	return cmd_ubifs_umount();
}
#else
static int mount_ubifs(char *mtdpart, char *ubivol)
{
	debug("Error: Cannot load image: no UBIFS support\n");
	return -ENOSYS;
}
#endif

static int select_fs_dev(struct device_platdata *plat)
{
	int ret;

	if (plat->phandlepart.phandle) {
		ofnode node;

		node = ofnode_get_by_phandle(plat->phandlepart.phandle);

		struct udevice *dev;

		ret = device_get_global_by_ofnode(node, &dev);
		if (!ret) {
			struct blk_desc *desc = blk_get_by_device(dev);
			if (desc) {
				ret = fs_set_blk_dev_with_part(desc,
					plat->phandlepart.partition);
			} else {
				debug("%s: No device found\n", __func__);
				return -ENODEV;
			}
		}
	} else if (plat->mtdpart && plat->ubivol) {
		ret = mount_ubifs(plat->mtdpart, plat->ubivol);
		if (ret)
			return ret;

		ret = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
	} else {
		debug("Error: unsupported storage device.\n");
		return -ENODEV;
	}

	if (ret)
		debug("Error: could not access storage.\n");

	return ret;
}

/**
 * _request_firmware_prepare - Prepare firmware struct.
 *
 * @name: Name of firmware file.
 * @dbuf: Address of buffer to load firmware into.
 * @size: Size of buffer.
 * @offset: Offset of a file for start reading into buffer.
 * @firmwarep: Pointer to pointer to firmware image.
 *
 * Return: Negative value if fail, 0 for successful.
 */
static int _request_firmware_prepare(const char *name, void *dbuf,
				    size_t size, u32 offset,
				    struct firmware **firmwarep)
{
	if (!name || name[0] == '\0')
		return -EINVAL;

	/* No memory allocation is required if *firmwarep is allocated */
	if (!(*firmwarep)) {
		(*firmwarep) = calloc(1, sizeof(struct firmware));
		if (!(*firmwarep))
			return -ENOMEM;

		(*firmwarep)->priv = calloc(1, sizeof(struct firmware_priv));
		if (!(*firmwarep)->priv) {
			free(*firmwarep);
			return -ENOMEM;
		}
	} else if (!(*firmwarep)->priv) {
		(*firmwarep)->priv = calloc(1, sizeof(struct firmware_priv));
		if (!(*firmwarep)->priv) {
			free(*firmwarep);
			return -ENOMEM;
		}
	}

	((struct firmware_priv *)((*firmwarep)->priv))->name = name;
	((struct firmware_priv *)((*firmwarep)->priv))->offset = offset;
	(*firmwarep)->data = dbuf;
	(*firmwarep)->size = size;

	return 0;
}

/**
 * release_firmware - Release the resource associated with a firmware image
 * @firmware: Firmware resource to release
 */
void release_firmware(struct firmware *firmware)
{
	if (firmware) {
		if (firmware->priv) {
			free(firmware->priv);
			firmware->priv = NULL;
		}
		free(firmware);
	}
}

/**
 * fw_get_filesystem_firmware - load firmware into an allocated buffer.
 * @plat: Platform data such as storage and partition firmware loading from.
 * @firmware: pointer to firmware image.
 *
 * Return: Size of total read, negative value when error.
 */
static int fw_get_filesystem_firmware(struct device_platdata *plat,
				     struct firmware *firmware)
{
	struct firmware_priv *fw_priv = NULL;
	loff_t actread;
	char *storage_interface, *dev_part, *ubi_mtdpart, *ubi_volume;
	int ret;

	storage_interface = env_get("storage_interface");
	dev_part = env_get("fw_dev_part");
	ubi_mtdpart = env_get("fw_ubi_mtdpart");
	ubi_volume = env_get("fw_ubi_volume");

	if (storage_interface && dev_part) {
		ret = fs_set_blk_dev(storage_interface, dev_part, FS_TYPE_ANY);
	} else if (storage_interface && ubi_mtdpart && ubi_volume) {
		ret = mount_ubifs(ubi_mtdpart, ubi_volume);
		if (ret)
			return ret;

		if (!strcmp("ubi", storage_interface))
			ret = fs_set_blk_dev(storage_interface, NULL,
				FS_TYPE_UBIFS);
		else
			ret = -ENODEV;
	} else {
		ret = select_fs_dev(plat);
	}

	if (ret)
		goto out;

	fw_priv = firmware->priv;

	ret = fs_read(fw_priv->name, (ulong)map_to_sysmem(firmware->data),
			fw_priv->offset, firmware->size, &actread);

	if (ret) {
		debug("Error: %d Failed to read %s from flash %lld != %zu.\n",
		      ret, fw_priv->name, actread, firmware->size);
	} else {
		ret = actread;
	}

out:
#ifdef CONFIG_CMD_UBIFS
	umount_ubifs();
#endif
	return ret;
}

/**
 * request_firmware_into_buf - Load firmware into a previously allocated buffer.
 * @plat: Platform data such as storage and partition firmware loading from.
 * @name: Name of firmware file.
 * @buf: Address of buffer to load firmware into.
 * @size: Size of buffer.
 * @offset: Offset of a file for start reading into buffer.
 * @firmwarep: Pointer to firmware image.
 *
 * The firmware is loaded directly into the buffer pointed to by @buf and
 * the @firmwarep data member is pointed at @buf.
 *
 * Return: Size of total read, negative value when error.
 */
int request_firmware_into_buf(struct device_platdata *plat,
			      const char *name,
			      void *buf, size_t size, u32 offset,
			      struct firmware **firmwarep)
{
	int ret;

	if (!plat)
		return -EINVAL;

	ret = _request_firmware_prepare(name, buf, size, offset, firmwarep);
	if (ret < 0) /* error */
		return ret;

	ret = fw_get_filesystem_firmware(plat, *firmwarep);

	return ret;
}

static int fs_loader_ofdata_to_platdata(struct udevice *dev)
{
	const char *fs_loader_path;
	u32 phandlepart[2];

	fs_loader_path = ofnode_get_chosen_prop("firmware-loader");

	if (fs_loader_path) {
		ofnode fs_loader_node;

		fs_loader_node = ofnode_path(fs_loader_path);
		if (ofnode_valid(fs_loader_node)) {
			struct device_platdata *plat;
			plat = dev->platdata;

			if (!ofnode_read_u32_array(fs_loader_node,
						  "phandlepart",
						  phandlepart, 2)) {
				plat->phandlepart.phandle = phandlepart[0];
				plat->phandlepart.partition = phandlepart[1];
			}

			plat->mtdpart = (char *)ofnode_read_string(
					 fs_loader_node, "mtdpart");

			plat->ubivol = (char *)ofnode_read_string(
					 fs_loader_node, "ubivol");
		}
	}

	return 0;
}

static int fs_loader_probe(struct udevice *dev)
{
	return 0;
};

static const struct udevice_id fs_loader_ids[] = {
	{ .compatible = "u-boot,fs-loader"},
	{ }
};

U_BOOT_DRIVER(fs_loader) = {
	.name			= "fs-loader",
	.id			= UCLASS_FS_FIRMWARE_LOADER,
	.of_match		= fs_loader_ids,
	.probe			= fs_loader_probe,
	.ofdata_to_platdata	= fs_loader_ofdata_to_platdata,
	.platdata_auto_alloc_size	= sizeof(struct device_platdata),
};

UCLASS_DRIVER(fs_loader) = {
	.id		= UCLASS_FS_FIRMWARE_LOADER,
	.name		= "fs-loader",
};