Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 2 | /* |
| 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 Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <malloc.h> |
Heinrich Schuchardt | 8784b0b | 2020-07-22 17:46:02 +0200 | [diff] [blame] | 13 | #include <mapmem.h> |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 14 | #include <errno.h> |
| 15 | #include <dfu.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 16 | #include <linux/printk.h> |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 17 | |
| 18 | static int dfu_transfer_medium_ram(enum dfu_op op, struct dfu_entity *dfu, |
| 19 | u64 offset, void *buf, long *len) |
| 20 | { |
| 21 | if (dfu->layout != DFU_RAM_ADDR) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 22 | pr_err("unsupported layout: %s\n", dfu_get_layout(dfu->layout)); |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 23 | return -EINVAL; |
| 24 | } |
| 25 | |
| 26 | if (offset > dfu->data.ram.size) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 27 | pr_err("request exceeds allowed area\n"); |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 28 | return -EINVAL; |
| 29 | } |
| 30 | |
| 31 | if (op == DFU_OP_WRITE) |
Heinrich Schuchardt | 8784b0b | 2020-07-22 17:46:02 +0200 | [diff] [blame] | 32 | memcpy(map_sysmem(dfu->data.ram.start + offset, 0), buf, *len); |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 33 | else |
Heinrich Schuchardt | 8784b0b | 2020-07-22 17:46:02 +0200 | [diff] [blame] | 34 | memcpy(buf, map_sysmem(dfu->data.ram.start + offset, 0), *len); |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 35 | |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | static int dfu_write_medium_ram(struct dfu_entity *dfu, u64 offset, |
| 40 | void *buf, long *len) |
| 41 | { |
| 42 | return dfu_transfer_medium_ram(DFU_OP_WRITE, dfu, offset, buf, len); |
| 43 | } |
| 44 | |
Patrick Delaunay | f4e934f | 2017-07-19 16:39:23 +0200 | [diff] [blame] | 45 | int dfu_get_medium_size_ram(struct dfu_entity *dfu, u64 *size) |
Stephen Warren | 020e6f3 | 2014-06-11 12:47:27 -0600 | [diff] [blame] | 46 | { |
Patrick Delaunay | 5ea9b89 | 2017-07-19 16:39:22 +0200 | [diff] [blame] | 47 | *size = dfu->data.ram.size; |
| 48 | |
| 49 | return 0; |
Stephen Warren | 020e6f3 | 2014-06-11 12:47:27 -0600 | [diff] [blame] | 50 | } |
| 51 | |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 52 | static int dfu_read_medium_ram(struct dfu_entity *dfu, u64 offset, |
| 53 | void *buf, long *len) |
| 54 | { |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 55 | return dfu_transfer_medium_ram(DFU_OP_READ, dfu, offset, buf, len); |
| 56 | } |
| 57 | |
Masami Hiramatsu | fa4282a | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 58 | int dfu_fill_entity_ram(struct dfu_entity *dfu, char *devstr, char **argv, int argc) |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 59 | { |
Masami Hiramatsu | fa4282a | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 60 | char *s; |
Mugunthan V N | 4fb4743 | 2016-04-22 14:19:25 +0530 | [diff] [blame] | 61 | |
Masami Hiramatsu | fa4282a | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 62 | if (argc != 3) { |
| 63 | pr_err("Invalid number of arguments.\n"); |
| 64 | return -EINVAL; |
Mugunthan V N | 4fb4743 | 2016-04-22 14:19:25 +0530 | [diff] [blame] | 65 | } |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 66 | |
| 67 | dfu->dev_type = DFU_DEV_RAM; |
Mugunthan V N | 4fb4743 | 2016-04-22 14:19:25 +0530 | [diff] [blame] | 68 | if (strcmp(argv[0], "ram")) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 69 | pr_err("unsupported device: %s\n", argv[0]); |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 70 | return -ENODEV; |
| 71 | } |
| 72 | |
| 73 | dfu->layout = DFU_RAM_ADDR; |
Masami Hiramatsu | fa4282a | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 74 | dfu->data.ram.start = hextoul(argv[1], &s); |
| 75 | if (*s) |
| 76 | return -EINVAL; |
| 77 | dfu->data.ram.size = hextoul(argv[2], &s); |
| 78 | if (*s) |
| 79 | return -EINVAL; |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 80 | |
| 81 | dfu->write_medium = dfu_write_medium_ram; |
Stephen Warren | 020e6f3 | 2014-06-11 12:47:27 -0600 | [diff] [blame] | 82 | dfu->get_medium_size = dfu_get_medium_size_ram; |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 83 | dfu->read_medium = dfu_read_medium_ram; |
| 84 | |
| 85 | dfu->inited = 0; |
| 86 | |
| 87 | return 0; |
| 88 | } |