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> |
| 8 | #include <malloc.h> |
| 9 | #include <errno.h> |
| 10 | #include <dfu.h> |
| 11 | |
| 12 | int dfu_tftp_write(char *dfu_entity_name, unsigned int addr, unsigned int len, |
| 13 | char *interface, char *devstring) |
| 14 | { |
| 15 | char *s, *sb; |
| 16 | int alt_setting_num, ret; |
| 17 | struct dfu_entity *dfu; |
| 18 | |
| 19 | debug("%s: name: %s addr: 0x%x len: %d device: %s:%s\n", __func__, |
| 20 | dfu_entity_name, addr, len, interface, devstring); |
| 21 | |
| 22 | ret = dfu_init_env_entities(interface, devstring); |
| 23 | if (ret) |
| 24 | goto done; |
| 25 | |
| 26 | /* |
| 27 | * We need to copy name pointed by *dfu_entity_name since this text |
| 28 | * is the integral part of the FDT image. |
| 29 | * Any implicit modification (i.e. done by strsep()) will corrupt |
| 30 | * the FDT image and prevent other images to be stored. |
| 31 | */ |
| 32 | s = strdup(dfu_entity_name); |
| 33 | sb = s; |
| 34 | if (!s) { |
| 35 | ret = -ENOMEM; |
| 36 | goto done; |
| 37 | } |
| 38 | |
| 39 | strsep(&s, "@"); |
Marek Vasut | 36dd7e7 | 2018-02-16 16:41:19 +0100 | [diff] [blame] | 40 | debug("%s: image name: %s strlen: %zd\n", __func__, sb, strlen(sb)); |
Lukasz Majewski | 2d50d68 | 2015-08-24 00:21:45 +0200 | [diff] [blame] | 41 | |
| 42 | alt_setting_num = dfu_get_alt(sb); |
| 43 | free(sb); |
| 44 | if (alt_setting_num < 0) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 45 | pr_err("Alt setting [%d] to write not found!", |
Lukasz Majewski | 2d50d68 | 2015-08-24 00:21:45 +0200 | [diff] [blame] | 46 | alt_setting_num); |
| 47 | ret = -ENODEV; |
| 48 | goto done; |
| 49 | } |
| 50 | |
| 51 | dfu = dfu_get_entity(alt_setting_num); |
| 52 | if (!dfu) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 53 | pr_err("DFU entity for alt: %d not found!", alt_setting_num); |
Lukasz Majewski | 2d50d68 | 2015-08-24 00:21:45 +0200 | [diff] [blame] | 54 | ret = -ENODEV; |
| 55 | goto done; |
| 56 | } |
| 57 | |
Marek Vasut | 36dd7e7 | 2018-02-16 16:41:19 +0100 | [diff] [blame] | 58 | ret = dfu_write_from_mem_addr(dfu, (void *)(uintptr_t)addr, len); |
Lukasz Majewski | 2d50d68 | 2015-08-24 00:21:45 +0200 | [diff] [blame] | 59 | |
| 60 | done: |
| 61 | dfu_free_entities(); |
| 62 | |
| 63 | return ret; |
| 64 | } |