static int _copy(int argc, char **argv) { int src_block; int dst_block; char tmp_copy[SD_HC_BLOCK_SIZE]; if (argc != 3) { printf("usage: %s src_block dst_block\n", argv[0]); return -1; } src_block = atoi(argv[1]); dst_block = atoi(argv[2]); sd_rw_response_t rd_state; sdcard_spi_read_blocks(card, src_block, tmp_copy, SD_HC_BLOCK_SIZE, 1, &rd_state); if (rd_state != SD_RW_OK) { printf("read error %d (block %d)\n", rd_state, src_block); return -1; } sd_rw_response_t wr_state; sdcard_spi_write_blocks(card, dst_block, tmp_copy, SD_HC_BLOCK_SIZE, 1, &wr_state); if (wr_state != SD_RW_OK) { printf("write error %d (block %d)\n", wr_state, dst_block); return -2; } printf("copy block %d to %d [OK]\n", src_block, dst_block); return 0; }
/** * @brief reads sectors from disk * * @param[in] pdrv drive number to identify the drive * @param[out] buff Data buffer to store read data * @param[in] sector Start sector in LBA * @param[in] count Number of sectors to read * * @return RES_OK if no error occurred * @return RES_NOTRDY if data wasn't read completely */ DRESULT disk_read(BYTE pdrv, BYTE *buff, DWORD sector, UINT count) { sdcard_spi_t *card = get_sd_card(pdrv); if ((card != NULL) && card->init_done) { sd_rw_response_t state; if (count != sdcard_spi_read_blocks(card, sector, (char *)buff, SD_HC_BLOCK_SIZE, count, &state)) { printf("[ERROR] disk_read: sdcard_spi_read_blocks: %d\n", state); return RES_NOTRDY; } return RES_OK; } return RES_NOTRDY; }
static int _read(int argc, char **argv) { int blockaddr; int cnt; bool print_as_char = false; if ((argc == 3) || (argc == 4)) { blockaddr = atoi(argv[1]); cnt = atoi(argv[2]); if (argc == 4 && (strcmp("-c", argv[3]) == 0)) { print_as_char = true; } } else { printf("usage: %s blockaddr cnt [-c]\n", argv[0]); return -1; } int total_read = 0; while (total_read < cnt) { int chunk_blocks = cnt - total_read; if (chunk_blocks > MAX_BLOCKS_IN_BUFFER) { chunk_blocks = MAX_BLOCKS_IN_BUFFER; } sd_rw_response_t state; int chunks_read = sdcard_spi_read_blocks(card, blockaddr + total_read, buffer, SD_HC_BLOCK_SIZE, chunk_blocks, &state); if (state != SD_RW_OK) { printf("read error %d (block %d/%d)\n", state, total_read + chunks_read, cnt); return -1; } for (int i = 0; i < chunk_blocks * SD_HC_BLOCK_SIZE; i++) { if ((i % SD_HC_BLOCK_SIZE) == 0) { printf("BLOCK %d:\n", blockaddr + total_read + i / SD_HC_BLOCK_SIZE); } if (print_as_char) { if (buffer[i] >= FIRST_PRINTABLE_ASCII_CHAR) { printf("%c", buffer[i]); } else { printf(ASCII_UNPRINTABLE_REPLACEMENT); } } else { printf("%02x ", buffer[i]); } if ((i % BLOCK_PRINT_BYTES_PER_LINE) == (BLOCK_PRINT_BYTES_PER_LINE - 1)) { puts(""); /* line break after BLOCK_PRINT_BYTES_PER_LINE bytes */ } if ((i % SD_HC_BLOCK_SIZE) == (SD_HC_BLOCK_SIZE - 1)) { puts(""); /* empty line after each printed block */ } } total_read += chunks_read; } return 0; }