Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * dfu.c -- DFU back-end routines |
| 3 | * |
| 4 | * Copyright (C) 2012 Samsung Electronics |
| 5 | * author: Lukasz Majewski <l.majewski@samsung.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | #include <common.h> |
| 23 | #include <malloc.h> |
| 24 | #include <dfu.h> |
| 25 | |
| 26 | enum dfu_mmc_op { |
| 27 | DFU_OP_READ = 1, |
| 28 | DFU_OP_WRITE, |
| 29 | }; |
| 30 | |
| 31 | static int mmc_block_op(enum dfu_mmc_op op, struct dfu_entity *dfu, |
| 32 | void *buf, long *len) |
| 33 | { |
| 34 | char cmd_buf[DFU_CMD_BUF_SIZE]; |
| 35 | |
| 36 | sprintf(cmd_buf, "mmc %s 0x%x %x %x", |
| 37 | op == DFU_OP_READ ? "read" : "write", |
| 38 | (unsigned int) buf, |
| 39 | dfu->data.mmc.lba_start, |
| 40 | dfu->data.mmc.lba_size); |
| 41 | |
| 42 | if (op == DFU_OP_READ) |
| 43 | *len = dfu->data.mmc.lba_blk_size * dfu->data.mmc.lba_size; |
| 44 | |
| 45 | debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf); |
| 46 | return run_command(cmd_buf, 0); |
| 47 | } |
| 48 | |
| 49 | static inline int mmc_block_write(struct dfu_entity *dfu, void *buf, long *len) |
| 50 | { |
| 51 | return mmc_block_op(DFU_OP_WRITE, dfu, buf, len); |
| 52 | } |
| 53 | |
| 54 | static inline int mmc_block_read(struct dfu_entity *dfu, void *buf, long *len) |
| 55 | { |
| 56 | return mmc_block_op(DFU_OP_READ, dfu, buf, len); |
| 57 | } |
| 58 | |
| 59 | static int mmc_file_op(enum dfu_mmc_op op, struct dfu_entity *dfu, |
| 60 | void *buf, long *len) |
| 61 | { |
| 62 | char cmd_buf[DFU_CMD_BUF_SIZE]; |
| 63 | char *str_env; |
| 64 | int ret; |
| 65 | |
| 66 | sprintf(cmd_buf, "fat%s mmc %d:%d 0x%x %s %lx", |
| 67 | op == DFU_OP_READ ? "load" : "write", |
| 68 | dfu->data.mmc.dev, dfu->data.mmc.part, |
| 69 | (unsigned int) buf, dfu->name, *len); |
| 70 | |
| 71 | debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf); |
| 72 | |
| 73 | ret = run_command(cmd_buf, 0); |
| 74 | if (ret) { |
| 75 | puts("dfu: Read error!\n"); |
| 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | if (dfu->layout != DFU_RAW_ADDR) { |
| 80 | str_env = getenv("filesize"); |
| 81 | if (str_env == NULL) { |
| 82 | puts("dfu: Wrong file size!\n"); |
| 83 | return -1; |
| 84 | } |
| 85 | *len = simple_strtoul(str_env, NULL, 16); |
| 86 | } |
| 87 | |
| 88 | return ret; |
| 89 | } |
| 90 | |
| 91 | static inline int mmc_file_write(struct dfu_entity *dfu, void *buf, long *len) |
| 92 | { |
| 93 | return mmc_file_op(DFU_OP_WRITE, dfu, buf, len); |
| 94 | } |
| 95 | |
| 96 | static inline int mmc_file_read(struct dfu_entity *dfu, void *buf, long *len) |
| 97 | { |
| 98 | return mmc_file_op(DFU_OP_READ, dfu, buf, len); |
| 99 | } |
| 100 | |
| 101 | int dfu_write_medium_mmc(struct dfu_entity *dfu, void *buf, long *len) |
| 102 | { |
| 103 | int ret = -1; |
| 104 | |
| 105 | switch (dfu->layout) { |
| 106 | case DFU_RAW_ADDR: |
| 107 | ret = mmc_block_write(dfu, buf, len); |
| 108 | break; |
| 109 | case DFU_FS_FAT: |
| 110 | ret = mmc_file_write(dfu, buf, len); |
| 111 | break; |
| 112 | default: |
| 113 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 114 | dfu_get_layout(dfu->layout)); |
| 115 | } |
| 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | int dfu_read_medium_mmc(struct dfu_entity *dfu, void *buf, long *len) |
| 121 | { |
| 122 | int ret = -1; |
| 123 | |
| 124 | switch (dfu->layout) { |
| 125 | case DFU_RAW_ADDR: |
| 126 | ret = mmc_block_read(dfu, buf, len); |
| 127 | break; |
| 128 | case DFU_FS_FAT: |
| 129 | ret = mmc_file_read(dfu, buf, len); |
| 130 | break; |
| 131 | default: |
| 132 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 133 | dfu_get_layout(dfu->layout)); |
| 134 | } |
| 135 | |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s) |
| 140 | { |
| 141 | char *st; |
| 142 | |
| 143 | dfu->dev_type = DFU_DEV_MMC; |
| 144 | st = strsep(&s, " "); |
| 145 | if (!strcmp(st, "mmc")) { |
| 146 | dfu->layout = DFU_RAW_ADDR; |
| 147 | dfu->data.mmc.lba_start = simple_strtoul(s, &s, 16); |
| 148 | dfu->data.mmc.lba_size = simple_strtoul(++s, &s, 16); |
| 149 | dfu->data.mmc.lba_blk_size = get_mmc_blk_size(dfu->dev_num); |
| 150 | } else if (!strcmp(st, "fat")) { |
| 151 | dfu->layout = DFU_FS_FAT; |
| 152 | dfu->data.mmc.dev = simple_strtoul(s, &s, 10); |
| 153 | dfu->data.mmc.part = simple_strtoul(++s, &s, 10); |
| 154 | } else { |
| 155 | printf("%s: Memory layout (%s) not supported!\n", __func__, st); |
| 156 | } |
| 157 | |
| 158 | dfu->read_medium = dfu_read_medium_mmc; |
| 159 | dfu->write_medium = dfu_write_medium_mmc; |
| 160 | |
| 161 | return 0; |
| 162 | } |