Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 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) { |
| 21 | error("unsupported layout: %s\n", dfu_get_layout(dfu->layout)); |
| 22 | return -EINVAL; |
| 23 | } |
| 24 | |
| 25 | if (offset > dfu->data.ram.size) { |
| 26 | error("request exceeds allowed area\n"); |
| 27 | 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 | |
| 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 | |
Stephen Warren | 020e6f3 | 2014-06-11 12:47:27 -0600 | [diff] [blame] | 44 | long dfu_get_medium_size_ram(struct dfu_entity *dfu) |
| 45 | { |
| 46 | return dfu->data.ram.size; |
| 47 | } |
| 48 | |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 49 | static int dfu_read_medium_ram(struct dfu_entity *dfu, u64 offset, |
| 50 | void *buf, long *len) |
| 51 | { |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 52 | return dfu_transfer_medium_ram(DFU_OP_READ, dfu, offset, buf, len); |
| 53 | } |
| 54 | |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 55 | int dfu_fill_entity_ram(struct dfu_entity *dfu, char *devstr, char *s) |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 56 | { |
Mugunthan V N | 4fb4743 | 2016-04-22 14:19:25 +0530 | [diff] [blame] | 57 | const char *argv[3]; |
| 58 | const char **parg = argv; |
| 59 | |
| 60 | for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) { |
| 61 | *parg = strsep(&s, " "); |
| 62 | if (*parg == NULL) { |
| 63 | error("Invalid number of arguments.\n"); |
| 64 | return -ENODEV; |
| 65 | } |
| 66 | } |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 67 | |
| 68 | dfu->dev_type = DFU_DEV_RAM; |
Mugunthan V N | 4fb4743 | 2016-04-22 14:19:25 +0530 | [diff] [blame] | 69 | if (strcmp(argv[0], "ram")) { |
| 70 | error("unsupported device: %s\n", argv[0]); |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 71 | return -ENODEV; |
| 72 | } |
| 73 | |
| 74 | dfu->layout = DFU_RAM_ADDR; |
Stephen Warren | 62d6d1c | 2016-04-22 13:34:16 -0600 | [diff] [blame^] | 75 | dfu->data.ram.start = (void *)simple_strtoul(argv[1], NULL, 16); |
| 76 | dfu->data.ram.size = simple_strtoul(argv[2], NULL, 16); |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 77 | |
| 78 | dfu->write_medium = dfu_write_medium_ram; |
Stephen Warren | 020e6f3 | 2014-06-11 12:47:27 -0600 | [diff] [blame] | 79 | dfu->get_medium_size = dfu_get_medium_size_ram; |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 80 | dfu->read_medium = dfu_read_medium_ram; |
| 81 | |
| 82 | dfu->inited = 0; |
| 83 | |
| 84 | return 0; |
| 85 | } |