Caleb Connolly | 5e602aa | 2023-12-06 22:01:30 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * DFU SCSI backend (based on MMC backend). |
| 4 | * |
| 5 | * Copyright (C) 2012 Samsung Electronics |
| 6 | * author: Lukasz Majewski <l.majewski@samsung.com> |
| 7 | * Copyright (C) 2032 Linaro Ltd. |
| 8 | */ |
| 9 | |
| 10 | #define LOG_DEBUG |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <log.h> |
| 14 | #include <malloc.h> |
| 15 | #include <errno.h> |
| 16 | #include <div64.h> |
| 17 | #include <dfu.h> |
| 18 | #include <ext4fs.h> |
| 19 | #include <fat.h> |
| 20 | #include <scsi.h> |
| 21 | #include <part.h> |
| 22 | #include <command.h> |
| 23 | #include <linux/printk.h> |
| 24 | |
| 25 | static unsigned char *dfu_file_buf; |
| 26 | static u64 dfu_file_buf_len; |
| 27 | static u64 dfu_file_buf_offset; |
| 28 | |
| 29 | /* No safer way to do this?? dev should be the scsi device */ |
| 30 | #define scsi_get_blk_desc(dev) \ |
| 31 | ((struct blk_desc *)dev_get_uclass_plat(dev)) |
| 32 | |
| 33 | #define find_scsi_device(dev_num, scsi) \ |
| 34 | blk_get_device(UCLASS_SCSI, dev_num, scsi) |
| 35 | |
| 36 | |
| 37 | static int scsi_block_op(enum dfu_op op, struct dfu_entity *dfu, |
| 38 | u64 offset, void *buf, long *len) |
| 39 | { |
| 40 | struct udevice *scsi; |
| 41 | u32 blk_start, blk_count, n = 0; |
| 42 | int ret; |
| 43 | |
| 44 | ret = find_scsi_device(dfu->data.scsi.lun, &scsi); |
| 45 | if (ret < 0) { |
| 46 | pr_err("Device SCSI %d - not found!", dfu->data.scsi.lun); |
| 47 | return -ENODEV; |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * We must ensure that we work in lba_blk_size chunks, so ALIGN |
| 52 | * this value. |
| 53 | */ |
| 54 | *len = ALIGN(*len, dfu->data.scsi.lba_blk_size); |
| 55 | |
| 56 | blk_start = dfu->data.scsi.lba_start + |
| 57 | (u32)lldiv(offset, dfu->data.scsi.lba_blk_size); |
| 58 | blk_count = *len / dfu->data.scsi.lba_blk_size; |
| 59 | if (blk_start + blk_count > |
| 60 | dfu->data.scsi.lba_start + dfu->data.scsi.lba_size) { |
| 61 | puts("Request would exceed designated area!\n"); |
| 62 | return -EINVAL; |
| 63 | } |
| 64 | |
| 65 | debug("%s: %s dev: %d start: %d cnt: %d buf: 0x%p\n", __func__, |
| 66 | op == DFU_OP_READ ? "scsi READ" : "scsi WRITE", |
| 67 | dfu->data.scsi.lun, blk_start, blk_count, buf); |
| 68 | switch (op) { |
| 69 | case DFU_OP_READ: |
| 70 | n = blk_dread(scsi_get_blk_desc(scsi), blk_start, blk_count, buf); |
| 71 | break; |
| 72 | case DFU_OP_WRITE: |
| 73 | n = blk_dwrite(scsi_get_blk_desc(scsi), blk_start, blk_count, |
| 74 | buf); |
| 75 | break; |
| 76 | default: |
| 77 | pr_err("Operation not supported\n"); |
| 78 | } |
| 79 | |
| 80 | if (n != blk_count) { |
| 81 | pr_err("scsi block operation failed"); |
| 82 | return -EIO; |
| 83 | } |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static int scsi_file_op(enum dfu_op op, struct dfu_entity *dfu, |
| 89 | u64 offset, void *buf, u64 *len) |
| 90 | { |
| 91 | char dev_part_str[8]; |
| 92 | int ret; |
| 93 | int fstype; |
| 94 | loff_t size = 0; |
| 95 | |
| 96 | switch (dfu->layout) { |
| 97 | case DFU_FS_FAT: |
| 98 | fstype = FS_TYPE_FAT; |
| 99 | break; |
| 100 | case DFU_FS_EXT4: |
| 101 | fstype = FS_TYPE_EXT; |
| 102 | break; |
| 103 | case DFU_SKIP: |
| 104 | return 0; |
| 105 | default: |
| 106 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 107 | dfu_get_layout(dfu->layout)); |
| 108 | return -1; |
| 109 | } |
| 110 | |
| 111 | snprintf(dev_part_str, sizeof(dev_part_str), "%d:%d", |
| 112 | dfu->data.scsi.dev, dfu->data.scsi.part); |
| 113 | |
| 114 | ret = fs_set_blk_dev("scsi", dev_part_str, fstype); |
| 115 | if (ret) { |
| 116 | puts("dfu: fs_set_blk_dev error!\n"); |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | switch (op) { |
| 121 | case DFU_OP_READ: |
| 122 | ret = fs_read(dfu->name, (size_t)buf, offset, *len, &size); |
| 123 | if (ret) { |
| 124 | puts("dfu: fs_read error!\n"); |
| 125 | return ret; |
| 126 | } |
| 127 | *len = size; |
| 128 | break; |
| 129 | case DFU_OP_WRITE: |
| 130 | ret = fs_write(dfu->name, (size_t)buf, offset, *len, &size); |
| 131 | if (ret) { |
| 132 | puts("dfu: fs_write error!\n"); |
| 133 | return ret; |
| 134 | } |
| 135 | break; |
| 136 | case DFU_OP_SIZE: |
| 137 | ret = fs_size(dfu->name, &size); |
| 138 | if (ret) { |
| 139 | puts("dfu: fs_size error!\n"); |
| 140 | return ret; |
| 141 | } |
| 142 | *len = size; |
| 143 | break; |
| 144 | default: |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | static int scsi_file_buf_write(struct dfu_entity *dfu, u64 offset, void *buf, long *len) |
| 152 | { |
| 153 | int ret = 0; |
| 154 | |
| 155 | if (offset == 0) { |
| 156 | dfu_file_buf_len = 0; |
| 157 | dfu_file_buf_offset = 0; |
| 158 | } |
| 159 | |
| 160 | /* Add to the current buffer. */ |
| 161 | if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) |
| 162 | *len = CONFIG_SYS_DFU_MAX_FILE_SIZE - dfu_file_buf_len; |
| 163 | memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len); |
| 164 | dfu_file_buf_len += *len; |
| 165 | |
| 166 | if (dfu_file_buf_len == CONFIG_SYS_DFU_MAX_FILE_SIZE) { |
| 167 | ret = scsi_file_op(DFU_OP_WRITE, dfu, dfu_file_buf_offset, |
| 168 | dfu_file_buf, &dfu_file_buf_len); |
| 169 | dfu_file_buf_offset += dfu_file_buf_len; |
| 170 | dfu_file_buf_len = 0; |
| 171 | } |
| 172 | |
| 173 | return ret; |
| 174 | } |
| 175 | |
| 176 | static int scsi_file_buf_write_finish(struct dfu_entity *dfu) |
| 177 | { |
| 178 | int ret = scsi_file_op(DFU_OP_WRITE, dfu, dfu_file_buf_offset, |
| 179 | dfu_file_buf, &dfu_file_buf_len); |
| 180 | |
| 181 | /* Now that we're done */ |
| 182 | dfu_file_buf_len = 0; |
| 183 | dfu_file_buf_offset = 0; |
| 184 | |
| 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | int dfu_write_medium_scsi(struct dfu_entity *dfu, |
| 189 | u64 offset, void *buf, long *len) |
| 190 | { |
| 191 | int ret = -1; |
| 192 | |
| 193 | switch (dfu->layout) { |
| 194 | case DFU_RAW_ADDR: |
| 195 | ret = scsi_block_op(DFU_OP_WRITE, dfu, offset, buf, len); |
| 196 | break; |
| 197 | case DFU_FS_FAT: |
| 198 | case DFU_FS_EXT4: |
| 199 | ret = scsi_file_buf_write(dfu, offset, buf, len); |
| 200 | break; |
| 201 | case DFU_SCRIPT: |
| 202 | ret = run_command_list(buf, *len, 0); |
| 203 | break; |
| 204 | case DFU_SKIP: |
| 205 | ret = 0; |
| 206 | break; |
| 207 | default: |
| 208 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 209 | dfu_get_layout(dfu->layout)); |
| 210 | } |
| 211 | |
| 212 | return ret; |
| 213 | } |
| 214 | |
| 215 | int dfu_flush_medium_scsi(struct dfu_entity *dfu) |
| 216 | { |
| 217 | int ret = 0; |
| 218 | |
| 219 | switch (dfu->layout) { |
| 220 | case DFU_FS_FAT: |
| 221 | case DFU_FS_EXT4: |
| 222 | ret = scsi_file_buf_write_finish(dfu); |
| 223 | break; |
| 224 | case DFU_SCRIPT: |
| 225 | /* script may have changed the dfu_alt_info */ |
| 226 | dfu_reinit_needed = true; |
| 227 | break; |
| 228 | case DFU_RAW_ADDR: |
| 229 | case DFU_SKIP: |
| 230 | break; |
| 231 | default: |
| 232 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 233 | dfu_get_layout(dfu->layout)); |
| 234 | } |
| 235 | |
| 236 | return ret; |
| 237 | } |
| 238 | |
| 239 | int dfu_get_medium_size_scsi(struct dfu_entity *dfu, u64 *size) |
| 240 | { |
| 241 | int ret; |
| 242 | |
| 243 | switch (dfu->layout) { |
| 244 | case DFU_RAW_ADDR: |
| 245 | *size = dfu->data.scsi.lba_size * dfu->data.scsi.lba_blk_size; |
| 246 | return 0; |
| 247 | case DFU_FS_FAT: |
| 248 | case DFU_FS_EXT4: |
| 249 | ret = scsi_file_op(DFU_OP_SIZE, dfu, 0, NULL, size); |
| 250 | if (ret < 0) |
| 251 | return ret; |
| 252 | return 0; |
| 253 | case DFU_SCRIPT: |
| 254 | case DFU_SKIP: |
| 255 | return 0; |
| 256 | default: |
| 257 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 258 | dfu_get_layout(dfu->layout)); |
| 259 | return -1; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | |
| 264 | static int scsi_file_buf_read(struct dfu_entity *dfu, u64 offset, void *buf, |
| 265 | long *len) |
| 266 | { |
| 267 | int ret; |
| 268 | |
| 269 | if (offset == 0 || offset >= dfu_file_buf_offset + dfu_file_buf_len || |
| 270 | offset + *len < dfu_file_buf_offset) { |
| 271 | u64 file_len = CONFIG_SYS_DFU_MAX_FILE_SIZE; |
| 272 | |
| 273 | ret = scsi_file_op(DFU_OP_READ, dfu, offset, dfu_file_buf, |
| 274 | &file_len); |
| 275 | if (ret < 0) |
| 276 | return ret; |
| 277 | dfu_file_buf_len = file_len; |
| 278 | dfu_file_buf_offset = offset; |
| 279 | } |
| 280 | if (offset + *len > dfu_file_buf_offset + dfu_file_buf_len) |
| 281 | return -EINVAL; |
| 282 | |
| 283 | /* Add to the current buffer. */ |
| 284 | memcpy(buf, dfu_file_buf + offset - dfu_file_buf_offset, *len); |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | int dfu_read_medium_scsi(struct dfu_entity *dfu, u64 offset, void *buf, |
| 290 | long *len) |
| 291 | { |
| 292 | int ret = -1; |
| 293 | |
| 294 | switch (dfu->layout) { |
| 295 | case DFU_RAW_ADDR: |
| 296 | ret = scsi_block_op(DFU_OP_READ, dfu, offset, buf, len); |
| 297 | break; |
| 298 | case DFU_FS_FAT: |
| 299 | case DFU_FS_EXT4: |
| 300 | ret = scsi_file_buf_read(dfu, offset, buf, len); |
| 301 | break; |
| 302 | default: |
| 303 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 304 | dfu_get_layout(dfu->layout)); |
| 305 | } |
| 306 | |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | void dfu_free_entity_scsi(struct dfu_entity *dfu) |
| 311 | { |
| 312 | if (dfu_file_buf) { |
| 313 | free(dfu_file_buf); |
| 314 | dfu_file_buf = NULL; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * @param s Parameter string containing space-separated arguments: |
| 320 | * 1st: |
| 321 | * raw (raw read/write) |
| 322 | * fat (files) |
| 323 | * ext4 (^) |
| 324 | * part (partition image) |
| 325 | * 2nd and 3rd: |
| 326 | * lba_start and lba_size, for raw write |
| 327 | * scsi_dev and scsi_part, for filesystems and part |
| 328 | * 4th (optional): |
| 329 | * scsipart <num> (access to HW escsi partitions) |
| 330 | */ |
| 331 | int dfu_fill_entity_scsi(struct dfu_entity *dfu, char *devstr, char **argv, int argc) |
| 332 | { |
| 333 | const char *entity_type; |
| 334 | ssize_t second_arg; |
| 335 | ssize_t third_arg = -1; |
| 336 | struct udevice *scsi; |
| 337 | struct blk_desc *blk_dev; |
| 338 | int ret; |
| 339 | char *s; |
| 340 | |
| 341 | if (argc < 2) { |
| 342 | pr_err("Need at least one argument\n"); |
| 343 | return -EINVAL; |
| 344 | } |
| 345 | |
| 346 | dfu->data.scsi.lun = dectoul(devstr, &s); |
| 347 | if (*s) |
| 348 | return -EINVAL; |
| 349 | |
| 350 | entity_type = argv[0]; |
| 351 | /* |
| 352 | * Base 0 means we'll accept (prefixed with 0x or 0) base 16, 8, |
| 353 | * with default 10. |
| 354 | */ |
| 355 | second_arg = simple_strtol(argv[1], &s, 0); |
| 356 | if (*s) |
| 357 | return -EINVAL; |
| 358 | if (argc >= 3) { |
| 359 | third_arg = simple_strtoul(argv[2], &s, 0); |
| 360 | if (*s) |
| 361 | return -EINVAL; |
| 362 | } |
| 363 | |
| 364 | if (scsi_scan(false)) { |
| 365 | pr_err("Couldn't init scsi device.\n"); |
| 366 | return -ENODEV; |
| 367 | } |
| 368 | |
| 369 | ret = find_scsi_device(dfu->data.scsi.lun, &scsi); |
| 370 | if (ret < 0) { |
| 371 | pr_err("Couldn't find scsi device no. %d.\n", |
| 372 | dfu->data.scsi.lun); |
| 373 | return -ENODEV; |
| 374 | } |
| 375 | |
| 376 | blk_dev = scsi_get_blk_desc(scsi); |
| 377 | if (!blk_dev) { |
| 378 | pr_err("Couldn't get block device for scsi device no. %d.\n", |
| 379 | dfu->data.scsi.lun); |
| 380 | return -ENODEV; |
| 381 | } |
| 382 | |
| 383 | /* if it's NOT a raw write */ |
| 384 | if (strcmp(entity_type, "raw")) { |
| 385 | dfu->data.scsi.dev = (second_arg != -1) ? second_arg : |
| 386 | dfu->data.scsi.lun; |
| 387 | dfu->data.scsi.part = third_arg; |
| 388 | } |
| 389 | |
| 390 | if (!strcmp(entity_type, "raw")) { |
| 391 | dfu->layout = DFU_RAW_ADDR; |
| 392 | dfu->data.scsi.lba_start = second_arg; |
| 393 | if (third_arg < 0) { |
| 394 | pr_err("raw requires two arguments\n"); |
| 395 | return -EINVAL; |
| 396 | } |
| 397 | dfu->data.scsi.lba_size = third_arg; |
| 398 | dfu->data.scsi.lba_blk_size = blk_dev->blksz; |
| 399 | |
| 400 | /* |
| 401 | * In case the size is zero (i.e. scsi raw 0x10 0), |
| 402 | * assume the user intends to use whole device. |
| 403 | */ |
| 404 | if (third_arg == 0) { |
| 405 | dfu->data.scsi.lba_size = blk_dev->lba; |
| 406 | } |
| 407 | |
| 408 | } else if (!strcmp(entity_type, "part")) { |
| 409 | struct disk_partition partinfo; |
| 410 | int scsipart = second_arg; |
| 411 | if (third_arg >= 0) { |
| 412 | pr_err("part only accepts one argument\n"); |
| 413 | return -EINVAL; |
| 414 | } |
| 415 | int offset = 0; |
| 416 | |
| 417 | if (part_get_info(blk_dev, scsipart, &partinfo) != 0) { |
| 418 | pr_err("Couldn't find part #%d on scsi device #%d\n", |
| 419 | scsipart, dfu->data.scsi.lun); |
| 420 | return -ENODEV; |
| 421 | } |
| 422 | |
| 423 | dfu->layout = DFU_RAW_ADDR; |
| 424 | dfu->data.scsi.lba_start = partinfo.start + offset; |
| 425 | dfu->data.scsi.lba_size = partinfo.size - offset; |
| 426 | dfu->data.scsi.lba_blk_size = partinfo.blksz; |
| 427 | } else if (!strcmp(entity_type, "fat")) { |
| 428 | dfu->layout = DFU_FS_FAT; |
| 429 | } else if (!strcmp(entity_type, "ext4")) { |
| 430 | dfu->layout = DFU_FS_EXT4; |
| 431 | } else if (!strcmp(entity_type, "skip")) { |
| 432 | dfu->layout = DFU_SKIP; |
| 433 | } else if (!strcmp(entity_type, "script")) { |
| 434 | dfu->layout = DFU_SCRIPT; |
| 435 | } else { |
| 436 | pr_err("Memory layout (%s) not supported!\n", entity_type); |
| 437 | return -ENODEV; |
| 438 | } |
| 439 | |
| 440 | dfu->dev_type = DFU_DEV_SCSI; |
| 441 | dfu->get_medium_size = dfu_get_medium_size_scsi; |
| 442 | dfu->read_medium = dfu_read_medium_scsi; |
| 443 | dfu->write_medium = dfu_write_medium_scsi; |
| 444 | dfu->flush_medium = dfu_flush_medium_scsi; |
| 445 | dfu->inited = 0; |
| 446 | dfu->free_entity = dfu_free_entity_scsi; |
| 447 | |
| 448 | /* Check if file buffer is ready */ |
| 449 | if (!dfu_file_buf) { |
| 450 | dfu_file_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, |
| 451 | CONFIG_SYS_DFU_MAX_FILE_SIZE); |
| 452 | if (!dfu_file_buf) { |
| 453 | pr_err("Could not memalign 0x%x bytes\n", |
| 454 | CONFIG_SYS_DFU_MAX_FILE_SIZE); |
| 455 | return -ENOMEM; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | return 0; |
| 460 | } |