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 | |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame^] | 66 | switch (dfu->layout) { |
| 67 | case DFU_FS_FAT: |
| 68 | sprintf(cmd_buf, "fat%s mmc %d:%d 0x%x %s %lx", |
| 69 | op == DFU_OP_READ ? "load" : "write", |
| 70 | dfu->data.mmc.dev, dfu->data.mmc.part, |
| 71 | (unsigned int) buf, dfu->name, *len); |
| 72 | break; |
| 73 | case DFU_FS_EXT4: |
| 74 | sprintf(cmd_buf, "ext4%s mmc %d:%d /%s 0x%x %ld", |
| 75 | op == DFU_OP_READ ? "load" : "write", |
| 76 | dfu->data.mmc.dev, dfu->data.mmc.part, |
| 77 | dfu->name, (unsigned int) buf, *len); |
| 78 | break; |
| 79 | default: |
| 80 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 81 | dfu_get_layout(dfu->layout)); |
| 82 | } |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 83 | |
| 84 | debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf); |
| 85 | |
| 86 | ret = run_command(cmd_buf, 0); |
| 87 | if (ret) { |
| 88 | puts("dfu: Read error!\n"); |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | if (dfu->layout != DFU_RAW_ADDR) { |
| 93 | str_env = getenv("filesize"); |
| 94 | if (str_env == NULL) { |
| 95 | puts("dfu: Wrong file size!\n"); |
| 96 | return -1; |
| 97 | } |
| 98 | *len = simple_strtoul(str_env, NULL, 16); |
| 99 | } |
| 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | static inline int mmc_file_write(struct dfu_entity *dfu, void *buf, long *len) |
| 105 | { |
| 106 | return mmc_file_op(DFU_OP_WRITE, dfu, buf, len); |
| 107 | } |
| 108 | |
| 109 | static inline int mmc_file_read(struct dfu_entity *dfu, void *buf, long *len) |
| 110 | { |
| 111 | return mmc_file_op(DFU_OP_READ, dfu, buf, len); |
| 112 | } |
| 113 | |
| 114 | int dfu_write_medium_mmc(struct dfu_entity *dfu, void *buf, long *len) |
| 115 | { |
| 116 | int ret = -1; |
| 117 | |
| 118 | switch (dfu->layout) { |
| 119 | case DFU_RAW_ADDR: |
| 120 | ret = mmc_block_write(dfu, buf, len); |
| 121 | break; |
| 122 | case DFU_FS_FAT: |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame^] | 123 | case DFU_FS_EXT4: |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 124 | ret = mmc_file_write(dfu, buf, len); |
| 125 | break; |
| 126 | default: |
| 127 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 128 | dfu_get_layout(dfu->layout)); |
| 129 | } |
| 130 | |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | int dfu_read_medium_mmc(struct dfu_entity *dfu, void *buf, long *len) |
| 135 | { |
| 136 | int ret = -1; |
| 137 | |
| 138 | switch (dfu->layout) { |
| 139 | case DFU_RAW_ADDR: |
| 140 | ret = mmc_block_read(dfu, buf, len); |
| 141 | break; |
| 142 | case DFU_FS_FAT: |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame^] | 143 | case DFU_FS_EXT4: |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 144 | ret = mmc_file_read(dfu, buf, len); |
| 145 | break; |
| 146 | default: |
| 147 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 148 | dfu_get_layout(dfu->layout)); |
| 149 | } |
| 150 | |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s) |
| 155 | { |
| 156 | char *st; |
| 157 | |
| 158 | dfu->dev_type = DFU_DEV_MMC; |
| 159 | st = strsep(&s, " "); |
| 160 | if (!strcmp(st, "mmc")) { |
| 161 | dfu->layout = DFU_RAW_ADDR; |
| 162 | dfu->data.mmc.lba_start = simple_strtoul(s, &s, 16); |
| 163 | dfu->data.mmc.lba_size = simple_strtoul(++s, &s, 16); |
| 164 | dfu->data.mmc.lba_blk_size = get_mmc_blk_size(dfu->dev_num); |
| 165 | } else if (!strcmp(st, "fat")) { |
| 166 | dfu->layout = DFU_FS_FAT; |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame^] | 167 | } else if (!strcmp(st, "ext4")) { |
| 168 | dfu->layout = DFU_FS_EXT4; |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 169 | } else { |
| 170 | printf("%s: Memory layout (%s) not supported!\n", __func__, st); |
| 171 | } |
| 172 | |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame^] | 173 | if (dfu->layout == DFU_FS_EXT4 || dfu->layout == DFU_FS_FAT) { |
| 174 | dfu->data.mmc.dev = simple_strtoul(s, &s, 10); |
| 175 | dfu->data.mmc.part = simple_strtoul(++s, &s, 10); |
| 176 | } |
| 177 | |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 178 | dfu->read_medium = dfu_read_medium_mmc; |
| 179 | dfu->write_medium = dfu_write_medium_mmc; |
| 180 | |
| 181 | return 0; |
| 182 | } |