blob: 2b5e05a913eeda5f4ed1b4a8ae501de78ee0d263 [file] [log] [blame]
Afzal Mohammeda9479f02013-09-18 01:15:24 +05301/*
2 * (C) Copyright 2013
3 * Afzal Mohammed <afzal.mohd.ma@gmail.com>
4 *
5 * Reference: dfu_mmc.c
6 * Copyright (C) 2012 Samsung Electronics
7 * author: Lukasz Majewski <l.majewski@samsung.com>
8 *
9 * SPDX-License-Identifier: GPL-2.0+
10 */
11
12#include <common.h>
13#include <malloc.h>
14#include <errno.h>
15#include <dfu.h>
16
17static int dfu_transfer_medium_ram(enum dfu_op op, struct dfu_entity *dfu,
18 u64 offset, void *buf, long *len)
19{
20 if (dfu->layout != DFU_RAM_ADDR) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090021 pr_err("unsupported layout: %s\n", dfu_get_layout(dfu->layout));
Afzal Mohammeda9479f02013-09-18 01:15:24 +053022 return -EINVAL;
23 }
24
25 if (offset > dfu->data.ram.size) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090026 pr_err("request exceeds allowed area\n");
Afzal Mohammeda9479f02013-09-18 01:15:24 +053027 return -EINVAL;
28 }
29
30 if (op == DFU_OP_WRITE)
31 memcpy(dfu->data.ram.start + offset, buf, *len);
32 else
33 memcpy(buf, dfu->data.ram.start + offset, *len);
34
35 return 0;
36}
37
38static int dfu_write_medium_ram(struct dfu_entity *dfu, u64 offset,
39 void *buf, long *len)
40{
41 return dfu_transfer_medium_ram(DFU_OP_WRITE, dfu, offset, buf, len);
42}
43
Patrick Delaunay15970d82017-07-19 16:39:23 +020044int dfu_get_medium_size_ram(struct dfu_entity *dfu, u64 *size)
Stephen Warren0e285b52014-06-11 12:47:27 -060045{
Patrick Delaunay4de51202017-07-19 16:39:22 +020046 *size = dfu->data.ram.size;
47
48 return 0;
Stephen Warren0e285b52014-06-11 12:47:27 -060049}
50
Afzal Mohammeda9479f02013-09-18 01:15:24 +053051static int dfu_read_medium_ram(struct dfu_entity *dfu, u64 offset,
52 void *buf, long *len)
53{
Afzal Mohammeda9479f02013-09-18 01:15:24 +053054 return dfu_transfer_medium_ram(DFU_OP_READ, dfu, offset, buf, len);
55}
56
Stephen Warrendd648272014-06-11 16:03:33 -060057int dfu_fill_entity_ram(struct dfu_entity *dfu, char *devstr, char *s)
Afzal Mohammeda9479f02013-09-18 01:15:24 +053058{
Mugunthan V Ne1b0f6f2016-04-22 14:19:25 +053059 const char *argv[3];
60 const char **parg = argv;
61
62 for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
63 *parg = strsep(&s, " ");
64 if (*parg == NULL) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090065 pr_err("Invalid number of arguments.\n");
Mugunthan V Ne1b0f6f2016-04-22 14:19:25 +053066 return -ENODEV;
67 }
68 }
Afzal Mohammeda9479f02013-09-18 01:15:24 +053069
70 dfu->dev_type = DFU_DEV_RAM;
Mugunthan V Ne1b0f6f2016-04-22 14:19:25 +053071 if (strcmp(argv[0], "ram")) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090072 pr_err("unsupported device: %s\n", argv[0]);
Afzal Mohammeda9479f02013-09-18 01:15:24 +053073 return -ENODEV;
74 }
75
76 dfu->layout = DFU_RAM_ADDR;
Stephen Warren3517de62016-04-22 13:34:16 -060077 dfu->data.ram.start = (void *)simple_strtoul(argv[1], NULL, 16);
78 dfu->data.ram.size = simple_strtoul(argv[2], NULL, 16);
Afzal Mohammeda9479f02013-09-18 01:15:24 +053079
80 dfu->write_medium = dfu_write_medium_ram;
Stephen Warren0e285b52014-06-11 12:47:27 -060081 dfu->get_medium_size = dfu_get_medium_size_ram;
Afzal Mohammeda9479f02013-09-18 01:15:24 +053082 dfu->read_medium = dfu_read_medium_ram;
83
84 dfu->inited = 0;
85
86 return 0;
87}