Lukasz Majewski | cb383cd | 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> |
Pantelis Antoniou | 1b6ca18 | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 24 | #include <errno.h> |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 25 | #include <div64.h> |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 26 | #include <dfu.h> |
| 27 | |
| 28 | enum dfu_mmc_op { |
| 29 | DFU_OP_READ = 1, |
| 30 | DFU_OP_WRITE, |
| 31 | }; |
| 32 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 33 | static unsigned char __aligned(CONFIG_SYS_CACHELINE_SIZE) |
| 34 | dfu_file_buf[CONFIG_SYS_DFU_MAX_FILE_SIZE]; |
| 35 | static long dfu_file_buf_len; |
| 36 | |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 37 | static int mmc_block_op(enum dfu_mmc_op op, struct dfu_entity *dfu, |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 38 | u64 offset, void *buf, long *len) |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 39 | { |
| 40 | char cmd_buf[DFU_CMD_BUF_SIZE]; |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 41 | u32 blk_start, blk_count; |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 42 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 43 | /* |
| 44 | * We must ensure that we work in lba_blk_size chunks, so ALIGN |
| 45 | * this value. |
| 46 | */ |
| 47 | *len = ALIGN(*len, dfu->data.mmc.lba_blk_size); |
| 48 | |
| 49 | blk_start = dfu->data.mmc.lba_start + |
| 50 | (u32)lldiv(offset, dfu->data.mmc.lba_blk_size); |
| 51 | blk_count = *len / dfu->data.mmc.lba_blk_size; |
| 52 | if (blk_start + blk_count > |
| 53 | dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) { |
| 54 | puts("Request would exceed designated area!\n"); |
| 55 | return -EINVAL; |
| 56 | } |
| 57 | |
| 58 | sprintf(cmd_buf, "mmc %s %p %x %x", |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 59 | op == DFU_OP_READ ? "read" : "write", |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 60 | buf, blk_start, blk_count); |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 61 | |
| 62 | debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf); |
| 63 | return run_command(cmd_buf, 0); |
| 64 | } |
| 65 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 66 | static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len) |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 67 | { |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 68 | if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) { |
| 69 | dfu_file_buf_len = 0; |
| 70 | return -EINVAL; |
| 71 | } |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 72 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 73 | /* Add to the current buffer. */ |
| 74 | memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len); |
| 75 | dfu_file_buf_len += *len; |
| 76 | |
| 77 | return 0; |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static int mmc_file_op(enum dfu_mmc_op op, struct dfu_entity *dfu, |
| 81 | void *buf, long *len) |
| 82 | { |
| 83 | char cmd_buf[DFU_CMD_BUF_SIZE]; |
| 84 | char *str_env; |
| 85 | int ret; |
| 86 | |
Łukasz Majewski | 43e6627 | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 87 | switch (dfu->layout) { |
| 88 | case DFU_FS_FAT: |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 89 | sprintf(cmd_buf, "fat%s mmc %d:%d 0x%x %s", |
Łukasz Majewski | 43e6627 | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 90 | op == DFU_OP_READ ? "load" : "write", |
| 91 | dfu->data.mmc.dev, dfu->data.mmc.part, |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 92 | (unsigned int) buf, dfu->name); |
| 93 | if (op == DFU_OP_WRITE) |
| 94 | sprintf(cmd_buf + strlen(cmd_buf), " %lx", *len); |
Łukasz Majewski | 43e6627 | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 95 | break; |
| 96 | case DFU_FS_EXT4: |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 97 | sprintf(cmd_buf, "ext4%s mmc %d:%d 0x%x /%s", |
Łukasz Majewski | 43e6627 | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 98 | op == DFU_OP_READ ? "load" : "write", |
| 99 | dfu->data.mmc.dev, dfu->data.mmc.part, |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 100 | (unsigned int) buf, dfu->name); |
Łukasz Majewski | 43e6627 | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 101 | break; |
| 102 | default: |
| 103 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 104 | dfu_get_layout(dfu->layout)); |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 105 | return -1; |
Łukasz Majewski | 43e6627 | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 106 | } |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 107 | |
| 108 | debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf); |
| 109 | |
| 110 | ret = run_command(cmd_buf, 0); |
| 111 | if (ret) { |
| 112 | puts("dfu: Read error!\n"); |
| 113 | return ret; |
| 114 | } |
| 115 | |
Łukasz Majewski | 81c1d7b | 2012-08-23 23:33:56 +0000 | [diff] [blame] | 116 | if (dfu->layout != DFU_RAW_ADDR && op == DFU_OP_READ) { |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 117 | str_env = getenv("filesize"); |
| 118 | if (str_env == NULL) { |
| 119 | puts("dfu: Wrong file size!\n"); |
| 120 | return -1; |
| 121 | } |
| 122 | *len = simple_strtoul(str_env, NULL, 16); |
| 123 | } |
| 124 | |
| 125 | return ret; |
| 126 | } |
| 127 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 128 | int dfu_write_medium_mmc(struct dfu_entity *dfu, |
| 129 | u64 offset, void *buf, long *len) |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 130 | { |
| 131 | int ret = -1; |
| 132 | |
| 133 | switch (dfu->layout) { |
| 134 | case DFU_RAW_ADDR: |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 135 | ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len); |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 136 | break; |
| 137 | case DFU_FS_FAT: |
Łukasz Majewski | 43e6627 | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 138 | case DFU_FS_EXT4: |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 139 | ret = mmc_file_buffer(dfu, buf, len); |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 140 | break; |
| 141 | default: |
| 142 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 143 | dfu_get_layout(dfu->layout)); |
| 144 | } |
| 145 | |
| 146 | return ret; |
| 147 | } |
| 148 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 149 | int dfu_flush_medium_mmc(struct dfu_entity *dfu) |
| 150 | { |
| 151 | int ret = 0; |
| 152 | |
| 153 | if (dfu->layout != DFU_RAW_ADDR) { |
| 154 | /* Do stuff here. */ |
| 155 | ret = mmc_file_op(DFU_OP_WRITE, dfu, &dfu_file_buf, |
| 156 | &dfu_file_buf_len); |
| 157 | |
| 158 | /* Now that we're done */ |
| 159 | dfu_file_buf_len = 0; |
| 160 | } |
| 161 | |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf, |
| 166 | long *len) |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 167 | { |
| 168 | int ret = -1; |
| 169 | |
| 170 | switch (dfu->layout) { |
| 171 | case DFU_RAW_ADDR: |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 172 | ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len); |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 173 | break; |
| 174 | case DFU_FS_FAT: |
Łukasz Majewski | 43e6627 | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 175 | case DFU_FS_EXT4: |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 176 | ret = mmc_file_op(DFU_OP_READ, dfu, buf, len); |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 177 | break; |
| 178 | default: |
| 179 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 180 | dfu_get_layout(dfu->layout)); |
| 181 | } |
| 182 | |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s) |
| 187 | { |
Pantelis Antoniou | 1b6ca18 | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 188 | int dev, part; |
| 189 | struct mmc *mmc; |
| 190 | block_dev_desc_t *blk_dev; |
| 191 | disk_partition_t partinfo; |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 192 | char *st; |
| 193 | |
| 194 | dfu->dev_type = DFU_DEV_MMC; |
| 195 | st = strsep(&s, " "); |
| 196 | if (!strcmp(st, "mmc")) { |
| 197 | dfu->layout = DFU_RAW_ADDR; |
| 198 | dfu->data.mmc.lba_start = simple_strtoul(s, &s, 16); |
| 199 | dfu->data.mmc.lba_size = simple_strtoul(++s, &s, 16); |
| 200 | dfu->data.mmc.lba_blk_size = get_mmc_blk_size(dfu->dev_num); |
| 201 | } else if (!strcmp(st, "fat")) { |
| 202 | dfu->layout = DFU_FS_FAT; |
Łukasz Majewski | 43e6627 | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 203 | } else if (!strcmp(st, "ext4")) { |
| 204 | dfu->layout = DFU_FS_EXT4; |
Pantelis Antoniou | 1b6ca18 | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 205 | } else if (!strcmp(st, "part")) { |
| 206 | |
| 207 | dfu->layout = DFU_RAW_ADDR; |
| 208 | |
| 209 | dev = simple_strtoul(s, &s, 10); |
| 210 | s++; |
| 211 | part = simple_strtoul(s, &s, 10); |
| 212 | |
| 213 | mmc = find_mmc_device(dev); |
| 214 | if (mmc == NULL || mmc_init(mmc)) { |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 215 | printf("%s: could not find mmc device #%d!\n", |
| 216 | __func__, dev); |
Pantelis Antoniou | 1b6ca18 | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 217 | return -ENODEV; |
| 218 | } |
| 219 | |
| 220 | blk_dev = &mmc->block_dev; |
| 221 | if (get_partition_info(blk_dev, part, &partinfo) != 0) { |
| 222 | printf("%s: could not find partition #%d on mmc device #%d!\n", |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 223 | __func__, part, dev); |
Pantelis Antoniou | 1b6ca18 | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 224 | return -ENODEV; |
| 225 | } |
| 226 | |
| 227 | dfu->data.mmc.lba_start = partinfo.start; |
| 228 | dfu->data.mmc.lba_size = partinfo.size; |
| 229 | dfu->data.mmc.lba_blk_size = partinfo.blksz; |
| 230 | |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 231 | } else { |
| 232 | printf("%s: Memory layout (%s) not supported!\n", __func__, st); |
Pantelis Antoniou | 1b6ca18 | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 233 | return -ENODEV; |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 234 | } |
| 235 | |
Łukasz Majewski | 43e6627 | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 236 | if (dfu->layout == DFU_FS_EXT4 || dfu->layout == DFU_FS_FAT) { |
| 237 | dfu->data.mmc.dev = simple_strtoul(s, &s, 10); |
| 238 | dfu->data.mmc.part = simple_strtoul(++s, &s, 10); |
| 239 | } |
| 240 | |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 241 | dfu->read_medium = dfu_read_medium_mmc; |
| 242 | dfu->write_medium = dfu_write_medium_mmc; |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 243 | dfu->flush_medium = dfu_flush_medium_mmc; |
| 244 | |
| 245 | /* initial state */ |
| 246 | dfu->inited = 0; |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 247 | |
| 248 | return 0; |
| 249 | } |