Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | 2d50d68 | 2015-08-24 00:21:45 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 |
| 4 | * Lukasz Majewski <l.majewski@majess.pl> |
Lukasz Majewski | 2d50d68 | 2015-08-24 00:21:45 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Lukasz Majewski | 2d50d68 | 2015-08-24 00:21:45 +0200 | [diff] [blame] | 9 | #include <malloc.h> |
| 10 | #include <errno.h> |
| 11 | #include <dfu.h> |
| 12 | |
AKASHI Takahiro | 045fd8b | 2020-10-29 13:47:41 +0900 | [diff] [blame] | 13 | /** |
| 14 | * dfu_write_by_name() - write data to DFU medium |
| 15 | * @dfu_entity_name: Name of DFU entity to write |
| 16 | * @addr: Address of data buffer to write |
| 17 | * @len: Number of bytes |
| 18 | * @interface: Destination DFU medium (e.g. "mmc") |
| 19 | * @devstring: Instance number of destination DFU medium (e.g. "1") |
| 20 | * |
| 21 | * This function is storing data received on DFU supported medium which |
| 22 | * is specified by @dfu_entity_name. |
| 23 | * |
| 24 | * Return: 0 - on success, error code - otherwise |
| 25 | */ |
AKASHI Takahiro | 1c2d129 | 2020-10-29 13:47:42 +0900 | [diff] [blame] | 26 | int dfu_write_by_name(char *dfu_entity_name, void *addr, |
AKASHI Takahiro | 045fd8b | 2020-10-29 13:47:41 +0900 | [diff] [blame] | 27 | unsigned int len, char *interface, char *devstring) |
Lukasz Majewski | 2d50d68 | 2015-08-24 00:21:45 +0200 | [diff] [blame] | 28 | { |
| 29 | char *s, *sb; |
| 30 | int alt_setting_num, ret; |
| 31 | struct dfu_entity *dfu; |
| 32 | |
AKASHI Takahiro | 1c2d129 | 2020-10-29 13:47:42 +0900 | [diff] [blame] | 33 | debug("%s: name: %s addr: 0x%p len: %d device: %s:%s\n", __func__, |
Lukasz Majewski | 2d50d68 | 2015-08-24 00:21:45 +0200 | [diff] [blame] | 34 | dfu_entity_name, addr, len, interface, devstring); |
| 35 | |
| 36 | ret = dfu_init_env_entities(interface, devstring); |
| 37 | if (ret) |
| 38 | goto done; |
| 39 | |
| 40 | /* |
| 41 | * We need to copy name pointed by *dfu_entity_name since this text |
| 42 | * is the integral part of the FDT image. |
| 43 | * Any implicit modification (i.e. done by strsep()) will corrupt |
| 44 | * the FDT image and prevent other images to be stored. |
| 45 | */ |
| 46 | s = strdup(dfu_entity_name); |
| 47 | sb = s; |
| 48 | if (!s) { |
| 49 | ret = -ENOMEM; |
| 50 | goto done; |
| 51 | } |
| 52 | |
| 53 | strsep(&s, "@"); |
Marek Vasut | 36dd7e7 | 2018-02-16 16:41:19 +0100 | [diff] [blame] | 54 | debug("%s: image name: %s strlen: %zd\n", __func__, sb, strlen(sb)); |
Lukasz Majewski | 2d50d68 | 2015-08-24 00:21:45 +0200 | [diff] [blame] | 55 | |
| 56 | alt_setting_num = dfu_get_alt(sb); |
| 57 | free(sb); |
| 58 | if (alt_setting_num < 0) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 59 | pr_err("Alt setting [%d] to write not found!", |
AKASHI Takahiro | 045fd8b | 2020-10-29 13:47:41 +0900 | [diff] [blame] | 60 | alt_setting_num); |
Lukasz Majewski | 2d50d68 | 2015-08-24 00:21:45 +0200 | [diff] [blame] | 61 | ret = -ENODEV; |
| 62 | goto done; |
| 63 | } |
| 64 | |
| 65 | dfu = dfu_get_entity(alt_setting_num); |
| 66 | if (!dfu) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 67 | pr_err("DFU entity for alt: %d not found!", alt_setting_num); |
Lukasz Majewski | 2d50d68 | 2015-08-24 00:21:45 +0200 | [diff] [blame] | 68 | ret = -ENODEV; |
| 69 | goto done; |
| 70 | } |
| 71 | |
AKASHI Takahiro | 1c2d129 | 2020-10-29 13:47:42 +0900 | [diff] [blame] | 72 | ret = dfu_write_from_mem_addr(dfu, (void *)addr, len); |
Lukasz Majewski | 2d50d68 | 2015-08-24 00:21:45 +0200 | [diff] [blame] | 73 | |
| 74 | done: |
| 75 | dfu_free_entities(); |
| 76 | |
| 77 | return ret; |
| 78 | } |
AKASHI Takahiro | f234566 | 2020-10-29 13:47:52 +0900 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * dfu_write_by_alt() - write data to DFU medium |
| 82 | * @dfu_alt_num: DFU alt setting number |
| 83 | * @addr: Address of data buffer to write |
| 84 | * @len: Number of bytes |
| 85 | * @interface: Destination DFU medium (e.g. "mmc") |
| 86 | * @devstring: Instance number of destination DFU medium (e.g. "1") |
| 87 | * |
| 88 | * This function is storing data received on DFU supported medium which |
| 89 | * is specified by @dfu_alt_name. |
| 90 | * |
| 91 | * Return: 0 - on success, error code - otherwise |
| 92 | */ |
| 93 | int dfu_write_by_alt(int dfu_alt_num, void *addr, unsigned int len, |
| 94 | char *interface, char *devstring) |
| 95 | { |
| 96 | struct dfu_entity *dfu; |
| 97 | int ret; |
| 98 | |
| 99 | debug("%s: alt: %d addr: 0x%p len: %d device: %s:%s\n", __func__, |
| 100 | dfu_alt_num, addr, len, interface, devstring); |
| 101 | |
| 102 | ret = dfu_init_env_entities(interface, devstring); |
| 103 | if (ret) |
| 104 | goto done; |
| 105 | |
| 106 | if (dfu_alt_num < 0) { |
| 107 | pr_err("Invalid alt number: %d", dfu_alt_num); |
| 108 | ret = -ENODEV; |
| 109 | goto done; |
| 110 | } |
| 111 | |
| 112 | dfu = dfu_get_entity(dfu_alt_num); |
| 113 | if (!dfu) { |
| 114 | pr_err("DFU entity for alt: %d not found!", dfu_alt_num); |
| 115 | ret = -ENODEV; |
| 116 | goto done; |
| 117 | } |
| 118 | |
| 119 | ret = dfu_write_from_mem_addr(dfu, (void *)(uintptr_t)addr, len); |
| 120 | |
| 121 | done: |
| 122 | dfu_free_entities(); |
| 123 | |
| 124 | return ret; |
| 125 | } |