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