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