blob: cc98668e7a474081d098ccd9d530d958898c90a0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Afzal Mohammede3c687a2013-09-18 01:15:24 +05302/*
3 * (C) Copyright 2013
4 * Afzal Mohammed <afzal.mohd.ma@gmail.com>
5 *
6 * Reference: dfu_mmc.c
7 * Copyright (C) 2012 Samsung Electronics
8 * author: Lukasz Majewski <l.majewski@samsung.com>
Afzal Mohammede3c687a2013-09-18 01:15:24 +05309 */
10
11#include <common.h>
12#include <malloc.h>
13#include <errno.h>
14#include <dfu.h>
15
16static int dfu_transfer_medium_ram(enum dfu_op op, struct dfu_entity *dfu,
17 u64 offset, void *buf, long *len)
18{
19 if (dfu->layout != DFU_RAM_ADDR) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090020 pr_err("unsupported layout: %s\n", dfu_get_layout(dfu->layout));
Afzal Mohammede3c687a2013-09-18 01:15:24 +053021 return -EINVAL;
22 }
23
24 if (offset > dfu->data.ram.size) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090025 pr_err("request exceeds allowed area\n");
Afzal Mohammede3c687a2013-09-18 01:15:24 +053026 return -EINVAL;
27 }
28
29 if (op == DFU_OP_WRITE)
30 memcpy(dfu->data.ram.start + offset, buf, *len);
31 else
32 memcpy(buf, dfu->data.ram.start + offset, *len);
33
34 return 0;
35}
36
37static int dfu_write_medium_ram(struct dfu_entity *dfu, u64 offset,
38 void *buf, long *len)
39{
40 return dfu_transfer_medium_ram(DFU_OP_WRITE, dfu, offset, buf, len);
41}
42
Patrick Delaunayf4e934f2017-07-19 16:39:23 +020043int dfu_get_medium_size_ram(struct dfu_entity *dfu, u64 *size)
Stephen Warren020e6f32014-06-11 12:47:27 -060044{
Patrick Delaunay5ea9b892017-07-19 16:39:22 +020045 *size = dfu->data.ram.size;
46
47 return 0;
Stephen Warren020e6f32014-06-11 12:47:27 -060048}
49
Afzal Mohammede3c687a2013-09-18 01:15:24 +053050static int dfu_read_medium_ram(struct dfu_entity *dfu, u64 offset,
51 void *buf, long *len)
52{
Afzal Mohammede3c687a2013-09-18 01:15:24 +053053 return dfu_transfer_medium_ram(DFU_OP_READ, dfu, offset, buf, len);
54}
55
Stephen Warren4afe50f2014-06-11 16:03:33 -060056int dfu_fill_entity_ram(struct dfu_entity *dfu, char *devstr, char *s)
Afzal Mohammede3c687a2013-09-18 01:15:24 +053057{
Mugunthan V N4fb47432016-04-22 14:19:25 +053058 const char *argv[3];
59 const char **parg = argv;
60
61 for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
62 *parg = strsep(&s, " ");
63 if (*parg == NULL) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090064 pr_err("Invalid number of arguments.\n");
Mugunthan V N4fb47432016-04-22 14:19:25 +053065 return -ENODEV;
66 }
67 }
Afzal Mohammede3c687a2013-09-18 01:15:24 +053068
69 dfu->dev_type = DFU_DEV_RAM;
Mugunthan V N4fb47432016-04-22 14:19:25 +053070 if (strcmp(argv[0], "ram")) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090071 pr_err("unsupported device: %s\n", argv[0]);
Afzal Mohammede3c687a2013-09-18 01:15:24 +053072 return -ENODEV;
73 }
74
75 dfu->layout = DFU_RAM_ADDR;
Stephen Warren62d6d1c2016-04-22 13:34:16 -060076 dfu->data.ram.start = (void *)simple_strtoul(argv[1], NULL, 16);
77 dfu->data.ram.size = simple_strtoul(argv[2], NULL, 16);
Afzal Mohammede3c687a2013-09-18 01:15:24 +053078
79 dfu->write_medium = dfu_write_medium_ram;
Stephen Warren020e6f32014-06-11 12:47:27 -060080 dfu->get_medium_size = dfu_get_medium_size_ram;
Afzal Mohammede3c687a2013-09-18 01:15:24 +053081 dfu->read_medium = dfu_read_medium_ram;
82
83 dfu->inited = 0;
84
85 return 0;
86}