Changeset 1ee00b7 in mainline
- Timestamp:
- 2009-08-30T22:25:48Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- a830611
- Parents:
- ff62c6d
- Location:
- uspace
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/libblock/libblock.c
rff62c6d r1ee00b7 50 50 #include <adt/list.h> 51 51 #include <adt/hash_table.h> 52 #include <macros.h> 52 53 #include <mem.h> 53 54 … … 62 63 typedef struct { 63 64 fibril_mutex_t lock; 64 size_t block_size; /**< Block size. */65 size_t lblock_size; /**< Logical block size. */ 65 66 unsigned block_count; /**< Total number of blocks. */ 66 67 unsigned blocks_cached; /**< Number of cached blocks. */ … … 78 79 size_t com_size; 79 80 void *bb_buf; 80 off_t bb_off;81 size_t bb_size;81 bn_t bb_addr; 82 size_t pblock_size; /**< Physical block size. */ 82 83 cache_t *cache; 83 84 } devcon_t; 84 85 85 static int read_block(devcon_t *devcon, bn_t boff, size_t block_size); 86 static int write_block(devcon_t *devcon, bn_t boff, size_t block_size); 86 static int read_blocks(devcon_t *devcon, bn_t ba, size_t cnt); 87 static int write_blocks(devcon_t *devcon, bn_t ba, size_t cnt); 88 static size_t get_block_size(int dev_phone, size_t *bsize); 87 89 88 90 static devcon_t *devcon_search(dev_handle_t dev_handle) … … 102 104 } 103 105 104 static int devcon_add(dev_handle_t dev_handle, int dev_phone, void *com_area,105 size_t com_size)106 static int devcon_add(dev_handle_t dev_handle, int dev_phone, size_t bsize, 107 void *com_area, size_t com_size) 106 108 { 107 109 link_t *cur; 108 110 devcon_t *devcon; 111 112 if (com_size < bsize) 113 return EINVAL; 109 114 110 115 devcon = malloc(sizeof(devcon_t)); … … 119 124 devcon->com_size = com_size; 120 125 devcon->bb_buf = NULL; 121 devcon->bb_ off= 0;122 devcon-> bb_size = 0;126 devcon->bb_addr = 0; 127 devcon->pblock_size = bsize; 123 128 devcon->cache = NULL; 124 129 … … 149 154 int dev_phone; 150 155 void *com_area; 151 156 size_t bsize; 157 152 158 com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE, 153 159 MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); … … 169 175 return rc; 170 176 } 177 178 if (get_block_size(dev_phone, &bsize) != EOK) { 179 munmap(com_area, com_size); 180 ipc_hangup(dev_phone); 181 return rc; 182 } 171 183 172 rc = devcon_add(dev_handle, dev_phone, com_area, com_size);184 rc = devcon_add(dev_handle, dev_phone, bsize, com_area, com_size); 173 185 if (rc != EOK) { 174 186 munmap(com_area, com_size); … … 201 213 } 202 214 203 int block_bb_read(dev_handle_t dev_handle, off_t off, size_t size)215 int block_bb_read(dev_handle_t dev_handle, bn_t ba) 204 216 { 205 217 void *bb_buf; … … 211 223 if (devcon->bb_buf) 212 224 return EEXIST; 213 bb_buf = malloc( size);225 bb_buf = malloc(devcon->pblock_size); 214 226 if (!bb_buf) 215 227 return ENOMEM; 216 228 217 229 fibril_mutex_lock(&devcon->com_area_lock); 218 rc = read_block (devcon, 0, size);230 rc = read_blocks(devcon, 0, 1); 219 231 if (rc != EOK) { 220 232 fibril_mutex_unlock(&devcon->com_area_lock); … … 222 234 return rc; 223 235 } 224 memcpy(bb_buf, devcon->com_area, size);236 memcpy(bb_buf, devcon->com_area, devcon->pblock_size); 225 237 fibril_mutex_unlock(&devcon->com_area_lock); 226 238 227 239 devcon->bb_buf = bb_buf; 228 devcon->bb_off = off; 229 devcon->bb_size = size; 240 devcon->bb_addr = ba; 230 241 231 242 return EOK; … … 275 286 fibril_mutex_initialize(&cache->lock); 276 287 list_initialize(&cache->free_head); 277 cache-> block_size = size;288 cache->lblock_size = size; 278 289 cache->block_count = blocks; 279 290 cache->blocks_cached = 0; 280 291 cache->mode = mode; 292 293 /* No block size translation a.t.m. */ 294 assert(cache->lblock_size == devcon->pblock_size); 281 295 282 296 if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1, … … 368 382 if (!b) 369 383 goto recycle; 370 b->data = malloc(cache-> block_size);384 b->data = malloc(cache->lblock_size); 371 385 if (!b->data) { 372 386 free(b); … … 400 414 fibril_mutex_lock(&devcon->com_area_lock); 401 415 memcpy(devcon->com_area, b->data, b->size); 402 rc = write_block(devcon, b->boff, 403 cache->block_size); 416 rc = write_blocks(devcon, b->boff, 1); 404 417 fibril_mutex_unlock(&devcon->com_area_lock); 405 418 if (rc != EOK) { … … 437 450 block_initialize(b); 438 451 b->dev_handle = dev_handle; 439 b->size = cache-> block_size;452 b->size = cache->lblock_size; 440 453 b->boff = boff; 441 454 hash_table_insert(&cache->block_hash, &key, &b->hash_link); … … 455 468 */ 456 469 fibril_mutex_lock(&devcon->com_area_lock); 457 rc = read_block (devcon, b->boff, cache->block_size);458 memcpy(b->data, devcon->com_area, cache-> block_size);470 rc = read_blocks(devcon, b->boff, 1); 471 memcpy(b->data, devcon->com_area, cache->lblock_size); 459 472 fibril_mutex_unlock(&devcon->com_area_lock); 460 473 if (rc != EOK) … … 510 523 fibril_mutex_lock(&devcon->com_area_lock); 511 524 memcpy(devcon->com_area, block->data, block->size); 512 rc = write_block (devcon, block->boff, block->size);525 rc = write_blocks(devcon, block->boff, 1); 513 526 fibril_mutex_unlock(&devcon->com_area_lock); 514 527 block->dirty = false; … … 588 601 */ 589 602 int block_seqread(dev_handle_t dev_handle, off_t *bufpos, size_t *buflen, 590 off_t *pos, void *dst, size_t size , size_t block_size)603 off_t *pos, void *dst, size_t size) 591 604 { 592 605 off_t offset = 0; 593 606 size_t left = size; 594 devcon_t *devcon = devcon_search(dev_handle); 607 size_t block_size; 608 devcon_t *devcon; 609 610 devcon = devcon_search(dev_handle); 595 611 assert(devcon); 612 block_size = devcon->pblock_size; 596 613 597 614 fibril_mutex_lock(&devcon->com_area_lock); … … 620 637 int rc; 621 638 622 rc = read_block (devcon, *pos / block_size, block_size);639 rc = read_blocks(devcon, *pos / block_size, 1); 623 640 if (rc != EOK) { 624 641 fibril_mutex_unlock(&devcon->com_area_lock); … … 635 652 } 636 653 637 /** Read block from block device.654 /** Read blocks from block device. 638 655 * 639 656 * @param devcon Device connection. 640 * @param b off Block index.641 * @param block_size Block size.657 * @param ba Address of first block. 658 * @param cnt Number of blocks. 642 659 * @param src Buffer for storing the data. 643 660 * 644 661 * @return EOK on success or negative error code on failure. 645 662 */ 646 static int read_block(devcon_t *devcon, bn_t boff, size_t block_size) 647 { 648 ipcarg_t retval; 663 static int read_blocks(devcon_t *devcon, bn_t ba, size_t cnt) 664 { 649 665 int rc; 650 666 651 667 assert(devcon); 652 rc = async_req_2_1(devcon->dev_phone, BD_READ_BLOCK, boff, block_size, 653 &retval); 654 if ((rc != EOK) || (retval != EOK)) 655 return (rc != EOK ? rc : (int) retval); 656 657 return EOK; 668 rc = async_req_3_0(devcon->dev_phone, BD_READ_BLOCKS, LOWER32(ba), 669 UPPER32(ba), cnt); 670 return rc; 658 671 } 659 672 … … 661 674 * 662 675 * @param devcon Device connection. 663 * @param b off Block index.664 * @param block_size Block size.676 * @param ba Address of first block. 677 * @param cnt Number of blocks. 665 678 * @param src Buffer containing the data to write. 666 679 * 667 680 * @return EOK on success or negative error code on failure. 668 681 */ 669 static int write_block(devcon_t *devcon, bn_t boff, size_t block_size) 670 { 671 ipcarg_t retval; 682 static int write_blocks(devcon_t *devcon, bn_t ba, size_t cnt) 683 { 672 684 int rc; 673 685 674 686 assert(devcon); 675 rc = async_req_2_1(devcon->dev_phone, BD_WRITE_BLOCK, boff, block_size, 676 &retval); 677 if ((rc != EOK) || (retval != EOK)) 678 return (rc != EOK ? rc : (int) retval); 679 680 return EOK; 687 rc = async_req_3_0(devcon->dev_phone, BD_WRITE_BLOCKS, LOWER32(ba), 688 UPPER32(ba), cnt); 689 return rc; 690 } 691 692 /** Get block size used by the device. */ 693 static size_t get_block_size(int dev_phone, size_t *bsize) 694 { 695 ipcarg_t bs; 696 int rc; 697 698 rc = async_req_0_1(dev_phone, BD_GET_BLOCK_SIZE, &bs); 699 if (rc == EOK) 700 *bsize = (size_t) bs; 701 702 return rc; 681 703 } 682 704 -
uspace/lib/libblock/libblock.h
rff62c6d r1ee00b7 60 60 #define BLOCK_FLAGS_NOREAD 1 61 61 62 typedef u nsignedbn_t; /**< Block number type. */62 typedef uint64_t bn_t; /**< Block number type. */ 63 63 64 64 typedef struct block { … … 98 98 extern void block_fini(dev_handle_t); 99 99 100 extern int block_bb_read(dev_handle_t, off_t, size_t);100 extern int block_bb_read(dev_handle_t, bn_t); 101 101 extern void *block_bb_get(dev_handle_t); 102 102 … … 107 107 108 108 extern int block_seqread(dev_handle_t, off_t *, size_t *, off_t *, void *, 109 size_t , size_t);109 size_t); 110 110 111 111 #endif -
uspace/lib/libc/include/ipc/bd.h
rff62c6d r1ee00b7 39 39 40 40 typedef enum { 41 BD_READ_BLOCK = IPC_FIRST_USER_METHOD, 42 BD_WRITE_BLOCK 41 BD_GET_BLOCK_SIZE = IPC_FIRST_USER_METHOD, 42 BD_READ_BLOCKS, 43 BD_WRITE_BLOCKS 43 44 } bd_request_t; 44 45 -
uspace/srv/bd/ata_bd/ata_bd.c
rff62c6d r1ee00b7 62 62 #include <bool.h> 63 63 #include <task.h> 64 #include <macros.h> 64 65 65 66 #include "ata_bd.h" … … 86 87 static int ata_bd_init(void); 87 88 static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall); 88 static int ata_bd_r dwr(int disk_id, ipcarg_t method, off_t offset, size_t size,89 static int ata_bd_read_blocks(int disk_id, uint64_t ba, size_t cnt, 89 90 void *buf); 90 static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt, 91 static int ata_bd_write_blocks(int disk_id, uint64_t ba, size_t cnt, 92 const void *buf); 93 static int ata_bd_read_block(int disk_id, uint64_t ba, size_t cnt, 91 94 void *buf); 92 static int ata_bd_write_block(int disk_id, uint64_t b lk_idx, size_t blk_cnt,95 static int ata_bd_write_block(int disk_id, uint64_t ba, size_t cnt, 93 96 const void *buf); 94 97 static int disk_init(disk_t *d, int disk_id); 95 98 static int drive_identify(int drive_id, void *buf); 96 99 static void disk_print_summary(disk_t *d); 97 static int coord_calc(disk_t *d, uint64_t b lk_idx, block_coord_t *bc);100 static int coord_calc(disk_t *d, uint64_t ba, block_coord_t *bc); 98 101 static void coord_sc_program(const block_coord_t *bc, uint16_t scnt); 99 102 static int wait_status(unsigned set, unsigned n_reset, uint8_t *pstatus, … … 228 231 int flags; 229 232 int retval; 230 off_t idx;231 size_t size;233 uint64_t ba; 234 size_t cnt; 232 235 int disk_id, i; 233 236 … … 270 273 ipc_answer_0(callid, EOK); 271 274 return; 272 case BD_READ_BLOCK :273 case BD_WRITE_BLOCK:274 idx = IPC_GET_ARG1(call);275 size = IPC_GET_ARG2(call);276 if ( size > comm_size) {277 retval = E INVAL;275 case BD_READ_BLOCKS: 276 ba = MERGE_LOUP32(IPC_GET_ARG1(call), 277 IPC_GET_ARG2(call)); 278 cnt = IPC_GET_ARG3(call); 279 if (cnt * block_size > comm_size) { 280 retval = ELIMIT; 278 281 break; 279 282 } 280 retval = ata_bd_rdwr(disk_id, method, idx, 281 size, fs_va); 283 retval = ata_bd_read_blocks(disk_id, ba, cnt, fs_va); 282 284 break; 285 case BD_WRITE_BLOCKS: 286 ba = MERGE_LOUP32(IPC_GET_ARG1(call), 287 IPC_GET_ARG2(call)); 288 cnt = IPC_GET_ARG3(call); 289 if (cnt * block_size > comm_size) { 290 retval = ELIMIT; 291 break; 292 } 293 retval = ata_bd_write_blocks(disk_id, ba, cnt, fs_va); 294 break; 295 case BD_GET_BLOCK_SIZE: 296 ipc_answer_1(callid, EOK, block_size); 297 continue; 283 298 default: 284 299 retval = EINVAL; … … 373 388 } 374 389 375 /** Transfer a logical block from/to the device. 376 * 377 * @param disk_id Device index (0 or 1) 378 * @param method @c BD_READ_BLOCK or @c BD_WRITE_BLOCK 379 * @param blk_idx Index of the first block. 380 * @param size Size of the logical block. 381 * @param buf Data buffer. 382 * 383 * @return EOK on success, EIO on error. 384 */ 385 static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t blk_idx, size_t size, 386 void *buf) 387 { 390 /** Read multiple blocks from the device. */ 391 static int ata_bd_read_blocks(int disk_id, uint64_t ba, size_t cnt, 392 void *buf) { 393 388 394 int rc; 389 size_t now; 390 391 while (size > 0) { 392 now = size < block_size ? size : block_size; 393 if (now != block_size) 394 return EINVAL; 395 396 if (method == BD_READ_BLOCK) 397 rc = ata_bd_read_block(disk_id, blk_idx, 1, buf); 398 else 399 rc = ata_bd_write_block(disk_id, blk_idx, 1, buf); 400 395 396 while (cnt > 0) { 397 rc = ata_bd_read_block(disk_id, ba, 1, buf); 401 398 if (rc != EOK) 402 399 return rc; 403 400 401 ++ba; 402 --cnt; 404 403 buf += block_size; 405 blk_idx++; 406 407 if (size > block_size) 408 size -= block_size; 409 else 410 size = 0; 404 } 405 406 return EOK; 407 } 408 409 /** Write multiple blocks to the device. */ 410 static int ata_bd_write_blocks(int disk_id, uint64_t ba, size_t cnt, 411 const void *buf) { 412 413 int rc; 414 415 while (cnt > 0) { 416 rc = ata_bd_write_block(disk_id, ba, 1, buf); 417 if (rc != EOK) 418 return rc; 419 420 ++ba; 421 --cnt; 422 buf += block_size; 411 423 } 412 424 … … 466 478 * 467 479 * @param disk_id Device index (0 or 1) 468 * @param b lk_idx Index ofthe first block.469 * @param blk_cntNumber of blocks to transfer.480 * @param ba Address the first block. 481 * @param cnt Number of blocks to transfer. 470 482 * @param buf Buffer for holding the data. 471 483 * 472 484 * @return EOK on success, EIO on error. 473 485 */ 474 static int ata_bd_read_block(int disk_id, uint64_t b lk_idx, size_t blk_cnt,486 static int ata_bd_read_block(int disk_id, uint64_t ba, size_t blk_cnt, 475 487 void *buf) 476 488 { … … 486 498 487 499 /* Compute block coordinates. */ 488 if (coord_calc(d, b lk_idx, &bc) != EOK)500 if (coord_calc(d, ba, &bc) != EOK) 489 501 return EINVAL; 490 502 … … 541 553 * 542 554 * @param disk_id Device index (0 or 1) 543 * @param b lk_idx Indexof the first block.544 * @param blk_cntNumber of blocks to transfer.555 * @param ba Address of the first block. 556 * @param cnt Number of blocks to transfer. 545 557 * @param buf Buffer holding the data to write. 546 558 * 547 559 * @return EOK on success, EIO on error. 548 560 */ 549 static int ata_bd_write_block(int disk_id, uint64_t b lk_idx, size_t blk_cnt,561 static int ata_bd_write_block(int disk_id, uint64_t ba, size_t cnt, 550 562 const void *buf) 551 563 { … … 560 572 561 573 /* Compute block coordinates. */ 562 if (coord_calc(d, b lk_idx, &bc) != EOK)574 if (coord_calc(d, ba, &bc) != EOK) 563 575 return EINVAL; 564 576 … … 620 632 * @return EOK on success or EINVAL if block index is past end of device. 621 633 */ 622 static int coord_calc(disk_t *d, uint64_t b lk_idx, block_coord_t *bc)634 static int coord_calc(disk_t *d, uint64_t ba, block_coord_t *bc) 623 635 { 624 636 uint64_t c; … … 626 638 627 639 /* Check device bounds. */ 628 if (b lk_idx>= d->blocks)640 if (ba >= d->blocks) 629 641 return EINVAL; 630 642 … … 634 646 case am_chs: 635 647 /* Compute CHS coordinates. */ 636 c = b lk_idx/ (d->geom.heads * d->geom.sectors);637 idx = b lk_idx% (d->geom.heads * d->geom.sectors);648 c = ba / (d->geom.heads * d->geom.sectors); 649 idx = ba % (d->geom.heads * d->geom.sectors); 638 650 639 651 bc->cyl_lo = c & 0xff; … … 645 657 case am_lba28: 646 658 /* Compute LBA-28 coordinates. */ 647 bc->c0 = b lk_idx& 0xff; /* bits 0-7 */648 bc->c1 = (b lk_idx >> 8) & 0xff;/* bits 8-15 */649 bc->c2 = (b lk_idx>> 16) & 0xff; /* bits 16-23 */650 bc->h = (b lk_idx>> 24) & 0x0f; /* bits 24-27 */659 bc->c0 = ba & 0xff; /* bits 0-7 */ 660 bc->c1 = (ba >> 8) & 0xff; /* bits 8-15 */ 661 bc->c2 = (ba >> 16) & 0xff; /* bits 16-23 */ 662 bc->h = (ba >> 24) & 0x0f; /* bits 24-27 */ 651 663 break; 652 664 653 665 case am_lba48: 654 666 /* Compute LBA-48 coordinates. */ 655 bc->c0 = b lk_idx& 0xff; /* bits 0-7 */656 bc->c1 = (b lk_idx >> 8) & 0xff;/* bits 8-15 */657 bc->c2 = (b lk_idx>> 16) & 0xff; /* bits 16-23 */658 bc->c3 = (b lk_idx>> 24) & 0xff; /* bits 24-31 */659 bc->c4 = (b lk_idx>> 32) & 0xff; /* bits 32-39 */660 bc->c5 = (b lk_idx>> 40) & 0xff; /* bits 40-47 */667 bc->c0 = ba & 0xff; /* bits 0-7 */ 668 bc->c1 = (ba >> 8) & 0xff; /* bits 8-15 */ 669 bc->c2 = (ba >> 16) & 0xff; /* bits 16-23 */ 670 bc->c3 = (ba >> 24) & 0xff; /* bits 24-31 */ 671 bc->c4 = (ba >> 32) & 0xff; /* bits 32-39 */ 672 bc->c5 = (ba >> 40) & 0xff; /* bits 40-47 */ 661 673 bc->h = 0; 662 674 break; -
uspace/srv/bd/file_bd/file_bd.c
rff62c6d r1ee00b7 51 51 #include <bool.h> 52 52 #include <task.h> 53 #include <macros.h> 53 54 54 55 #define NAME "file_bd" 55 56 56 static size_t comm_size;57 static const size_t block_size = 512; 57 58 static FILE *img; 58 59 … … 62 63 static int file_bd_init(const char *fname); 63 64 static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall); 64 static int file_bd_read (off_t blk_idx, size_t size, void *buf);65 static int file_bd_write (off_t blk_idx, size_t size,void *buf);65 static int file_bd_read_blocks(uint64_t ba, size_t cnt, void *buf); 66 static int file_bd_write_blocks(uint64_t ba, size_t cnt, const void *buf); 66 67 67 68 int main(int argc, char **argv) … … 120 121 ipc_call_t call; 121 122 ipcarg_t method; 123 size_t comm_size; 122 124 int flags; 123 125 int retval; 124 off_t idx;125 size_t size;126 uint64_t ba; 127 size_t cnt; 126 128 127 129 /* Answer the IPC_M_CONNECT_ME_TO call. */ … … 149 151 ipc_answer_0(callid, EOK); 150 152 return; 151 case BD_READ_BLOCK :152 case BD_WRITE_BLOCK:153 idx = IPC_GET_ARG1(call);154 size = IPC_GET_ARG2(call);155 if ( size > comm_size) {156 retval = E INVAL;153 case BD_READ_BLOCKS: 154 ba = MERGE_LOUP32(IPC_GET_ARG1(call), 155 IPC_GET_ARG2(call)); 156 cnt = IPC_GET_ARG3(call); 157 if (cnt * block_size > comm_size) { 158 retval = ELIMIT; 157 159 break; 158 160 } 159 if (method == BD_READ_BLOCK) 160 retval = file_bd_read(idx, size, fs_va); 161 else 162 retval = file_bd_write(idx, size, fs_va); 161 retval = file_bd_read_blocks(ba, cnt, fs_va); 163 162 break; 163 case BD_WRITE_BLOCKS: 164 ba = MERGE_LOUP32(IPC_GET_ARG1(call), 165 IPC_GET_ARG2(call)); 166 cnt = IPC_GET_ARG3(call); 167 if (cnt * block_size > comm_size) { 168 retval = ELIMIT; 169 break; 170 } 171 retval = file_bd_write_blocks(ba, cnt, fs_va); 172 break; 173 case BD_GET_BLOCK_SIZE: 174 ipc_answer_1(callid, EOK, block_size); 175 continue; 164 176 default: 165 177 retval = EINVAL; … … 170 182 } 171 183 172 static int file_bd_read(off_t blk_idx, size_t size, void *buf) 184 /** Read blocks from the device. */ 185 static int file_bd_read_blocks(uint64_t ba, size_t cnt, void *buf) 173 186 { 174 187 size_t n_rd; … … 176 189 fibril_mutex_lock(&dev_lock); 177 190 178 fseek(img, b lk_idx *size, SEEK_SET);179 n_rd = fread(buf, 1, size, img);191 fseek(img, ba * block_size, SEEK_SET); 192 n_rd = fread(buf, block_size, cnt, img); 180 193 181 194 if (ferror(img)) { … … 186 199 fibril_mutex_unlock(&dev_lock); 187 200 188 if (n_rd < size)189 return EINVAL; /* Read beyond end of d isk*/201 if (n_rd < cnt) 202 return EINVAL; /* Read beyond end of device */ 190 203 191 204 return EOK; 192 205 } 193 206 194 static int file_bd_write(off_t blk_idx, size_t size, void *buf) 207 /** Write blocks to the device. */ 208 static int file_bd_write_blocks(uint64_t ba, size_t cnt, const void *buf) 195 209 { 196 210 size_t n_wr; … … 198 212 fibril_mutex_lock(&dev_lock); 199 213 200 fseek(img, b lk_idx *size, SEEK_SET);201 n_wr = fread(buf, 1, size, img);202 203 if (ferror(img) || n_wr < size) {214 fseek(img, ba * block_size, SEEK_SET); 215 n_wr = fread(buf, block_size, cnt, img); 216 217 if (ferror(img) || n_wr < cnt) { 204 218 fibril_mutex_unlock(&dev_lock); 205 219 return EIO; /* Write error */ -
uspace/srv/bd/gxe_bd/gxe_bd.c
rff62c6d r1ee00b7 47 47 #include <sys/types.h> 48 48 #include <errno.h> 49 #include <macros.h> 49 50 #include <task.h> 50 51 … … 97 98 static int gxe_bd_init(void); 98 99 static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall); 99 static int gx _bd_rdwr(int disk_id, ipcarg_t method, off_t offset, size_t size,100 static int gxe_bd_read_blocks(int disk_id, uint64_t ba, unsigned cnt, 100 101 void *buf); 101 static int gxe_bd_read_block(int disk_id, uint64_t offset, size_t size, 102 void *buf); 103 static int gxe_bd_write_block(int disk_id, uint64_t offset, size_t size, 102 static int gxe_bd_write_blocks(int disk_id, uint64_t ba, unsigned cnt, 104 103 const void *buf); 104 static int gxe_bd_read_block(int disk_id, uint64_t ba, void *buf); 105 static int gxe_bd_write_block(int disk_id, uint64_t ba, const void *buf); 105 106 106 107 int main(int argc, char **argv) … … 163 164 int flags; 164 165 int retval; 165 off_t idx;166 size_t size;166 uint64_t ba; 167 unsigned cnt; 167 168 int disk_id, i; 168 169 … … 185 186 186 187 if (!ipc_share_out_receive(&callid, &comm_size, &flags)) { 188 ipc_answer_0(callid, EHANGUP); 189 return; 190 } 191 192 if (comm_size < block_size) { 187 193 ipc_answer_0(callid, EHANGUP); 188 194 return; … … 205 211 ipc_answer_0(callid, EOK); 206 212 return; 207 case BD_READ_BLOCK :208 case BD_WRITE_BLOCK:209 idx = IPC_GET_ARG1(call);210 size = IPC_GET_ARG2(call);211 if ( size > comm_size) {212 retval = E INVAL;213 case BD_READ_BLOCKS: 214 ba = MERGE_LOUP32(IPC_GET_ARG1(call), 215 IPC_GET_ARG2(call)); 216 cnt = IPC_GET_ARG3(call); 217 if (cnt * block_size > comm_size) { 218 retval = ELIMIT; 213 219 break; 214 220 } 215 retval = gx_bd_rdwr(disk_id, method, idx * size, 216 size, fs_va); 221 retval = gxe_bd_read_blocks(disk_id, ba, cnt, fs_va); 217 222 break; 223 case BD_WRITE_BLOCKS: 224 ba = MERGE_LOUP32(IPC_GET_ARG1(call), 225 IPC_GET_ARG2(call)); 226 cnt = IPC_GET_ARG3(call); 227 if (cnt * block_size > comm_size) { 228 retval = ELIMIT; 229 break; 230 } 231 retval = gxe_bd_write_blocks(disk_id, ba, cnt, fs_va); 232 break; 233 case BD_GET_BLOCK_SIZE: 234 ipc_answer_1(callid, EOK, block_size); 235 continue; 218 236 default: 219 237 retval = EINVAL; … … 224 242 } 225 243 226 static int gx_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, size_t size, 227 void *buf) 228 { 244 /** Read multiple blocks from the device. */ 245 static int gxe_bd_read_blocks(int disk_id, uint64_t ba, unsigned cnt, 246 void *buf) { 247 229 248 int rc; 230 size_t now; 231 232 while (size > 0) { 233 now = size < block_size ? size : block_size; 234 235 if (method == BD_READ_BLOCK) 236 rc = gxe_bd_read_block(disk_id, offset, now, buf); 237 else 238 rc = gxe_bd_write_block(disk_id, offset, now, buf); 239 249 250 while (cnt > 0) { 251 rc = gxe_bd_read_block(disk_id, ba, buf); 240 252 if (rc != EOK) 241 253 return rc; 242 254 255 ++ba; 256 --cnt; 243 257 buf += block_size; 244 offset += block_size; 245 246 if (size > block_size) 247 size -= block_size; 248 else 249 size = 0; 250 } 251 252 return EOK; 253 } 254 255 static int gxe_bd_read_block(int disk_id, uint64_t offset, size_t size, 256 void *buf) 258 } 259 260 return EOK; 261 } 262 263 /** Write multiple blocks to the device. */ 264 static int gxe_bd_write_blocks(int disk_id, uint64_t ba, unsigned cnt, 265 const void *buf) { 266 267 int rc; 268 269 while (cnt > 0) { 270 rc = gxe_bd_write_block(disk_id, ba, buf); 271 if (rc != EOK) 272 return rc; 273 274 ++ba; 275 --cnt; 276 buf += block_size; 277 } 278 279 return EOK; 280 } 281 282 /** Read a block from the device. */ 283 static int gxe_bd_read_block(int disk_id, uint64_t ba, void *buf) 257 284 { 258 285 uint32_t status; 286 uint64_t byte_addr; 259 287 size_t i; 260 288 uint32_t w; 261 289 290 byte_addr = ba * block_size; 291 262 292 fibril_mutex_lock(&dev_lock[disk_id]); 263 pio_write_32(&dev->offset_lo, (uint32_t) offset);264 pio_write_32(&dev->offset_hi, offset>> 32);293 pio_write_32(&dev->offset_lo, (uint32_t) byte_addr); 294 pio_write_32(&dev->offset_hi, byte_addr >> 32); 265 295 pio_write_32(&dev->disk_id, disk_id); 266 296 pio_write_32(&dev->control, CTL_READ_START); … … 272 302 } 273 303 274 for (i = 0; i < size; i++) {304 for (i = 0; i < block_size; i++) { 275 305 ((uint8_t *) buf)[i] = w = pio_read_8(&dev->buffer[i]); 276 306 } … … 280 310 } 281 311 282 static int gxe_bd_write_block(int disk_id, uint64_t offset, size_t size, 283 312 /** Write a block to the device. */ 313 static int gxe_bd_write_block(int disk_id, uint64_t ba, const void *buf) 284 314 { 285 315 uint32_t status; 316 uint64_t byte_addr; 286 317 size_t i; 287 318 288 for (i = 0; i < size; i++) { 319 byte_addr = ba * block_size; 320 321 fibril_mutex_lock(&dev_lock[disk_id]); 322 323 for (i = 0; i < block_size; i++) { 289 324 pio_write_8(&dev->buffer[i], ((const uint8_t *) buf)[i]); 290 325 } 291 326 292 fibril_mutex_lock(&dev_lock[disk_id]); 293 pio_write_32(&dev->offset_lo, (uint32_t) offset); 294 pio_write_32(&dev->offset_hi, offset >> 32); 327 pio_write_32(&dev->offset_lo, (uint32_t) byte_addr); 328 pio_write_32(&dev->offset_hi, byte_addr >> 32); 295 329 pio_write_32(&dev->disk_id, disk_id); 296 330 pio_write_32(&dev->control, CTL_WRITE_START); -
uspace/srv/bd/rd/rd.c
rff62c6d r1ee00b7 55 55 #include <devmap.h> 56 56 #include <ipc/bd.h> 57 #include <macros.h> 57 58 58 59 #define NAME "rd" 59 60 60 /** Pointer to the ramdisk's image .*/61 /** Pointer to the ramdisk's image */ 61 62 static void *rd_addr; 62 /** Size of the ramdisk .*/63 /** Size of the ramdisk */ 63 64 static size_t rd_size; 65 66 /** Block size */ 67 static const size_t block_size = 512; 68 69 static int rd_read_blocks(uint64_t ba, size_t cnt, void *buf); 70 static int rd_write_blocks(uint64_t ba, size_t cnt, const void *buf); 64 71 65 72 /** … … 82 89 int retval; 83 90 void *fs_va = NULL; 84 off_t offset;85 size_t block_size;86 size_t maxblock_size;91 uint64_t ba; 92 size_t cnt; 93 size_t comm_size; 87 94 88 95 /* … … 95 102 */ 96 103 int flags; 97 if (ipc_share_out_receive(&callid, & maxblock_size, &flags)) {98 fs_va = as_get_mappable_page( maxblock_size);104 if (ipc_share_out_receive(&callid, &comm_size, &flags)) { 105 fs_va = as_get_mappable_page(comm_size); 99 106 if (fs_va) { 100 107 (void) ipc_share_out_finalize(callid, fs_va); … … 123 130 ipc_answer_0(callid, EOK); 124 131 return; 125 case BD_READ_BLOCK: 126 offset = IPC_GET_ARG1(call); 127 block_size = IPC_GET_ARG2(call); 128 if (block_size > maxblock_size) { 129 /* 130 * Maximum block size exceeded. 131 */ 132 case BD_READ_BLOCKS: 133 ba = MERGE_LOUP32(IPC_GET_ARG1(call), 134 IPC_GET_ARG2(call)); 135 cnt = IPC_GET_ARG3(call); 136 if (cnt * block_size > comm_size) { 132 137 retval = ELIMIT; 133 138 break; 134 139 } 135 if (offset * block_size > rd_size - block_size) { 136 /* 137 * Reading past the end of the device. 138 */ 140 retval = rd_read_blocks(ba, cnt, fs_va); 141 break; 142 case BD_WRITE_BLOCKS: 143 ba = MERGE_LOUP32(IPC_GET_ARG1(call), 144 IPC_GET_ARG2(call)); 145 cnt = IPC_GET_ARG3(call); 146 if (cnt * block_size > comm_size) { 139 147 retval = ELIMIT; 140 148 break; 141 149 } 142 fibril_rwlock_read_lock(&rd_lock); 143 memcpy(fs_va, rd_addr + offset * block_size, block_size); 144 fibril_rwlock_read_unlock(&rd_lock); 145 retval = EOK; 150 retval = rd_write_blocks(ba, cnt, fs_va); 146 151 break; 147 case BD_WRITE_BLOCK: 148 offset = IPC_GET_ARG1(call); 149 block_size = IPC_GET_ARG2(call); 150 if (block_size > maxblock_size) { 151 /* 152 * Maximum block size exceeded. 153 */ 154 retval = ELIMIT; 155 break; 156 } 157 if (offset * block_size > rd_size - block_size) { 158 /* 159 * Writing past the end of the device. 160 */ 161 retval = ELIMIT; 162 break; 163 } 164 fibril_rwlock_write_lock(&rd_lock); 165 memcpy(rd_addr + offset * block_size, fs_va, block_size); 166 fibril_rwlock_write_unlock(&rd_lock); 167 retval = EOK; 168 break; 152 case BD_GET_BLOCK_SIZE: 153 ipc_answer_1(callid, EOK, block_size); 154 continue; 169 155 default: 170 156 /* … … 181 167 } 182 168 169 /** Read blocks from the device. */ 170 static int rd_read_blocks(uint64_t ba, size_t cnt, void *buf) 171 { 172 if ((ba + cnt) * block_size > rd_size) { 173 /* Reading past the end of the device. */ 174 return ELIMIT; 175 } 176 177 fibril_rwlock_read_lock(&rd_lock); 178 memcpy(buf, rd_addr + ba * block_size, block_size * cnt); 179 fibril_rwlock_read_unlock(&rd_lock); 180 181 return EOK; 182 } 183 184 /** Write blocks to the device. */ 185 static int rd_write_blocks(uint64_t ba, size_t cnt, const void *buf) 186 { 187 if ((ba + cnt) * block_size > rd_size) { 188 /* Writing past the end of the device. */ 189 return ELIMIT; 190 } 191 192 fibril_rwlock_write_lock(&rd_lock); 193 memcpy(rd_addr + ba * block_size, buf, block_size * cnt); 194 fibril_rwlock_write_unlock(&rd_lock); 195 196 return EOK; 197 } 198 183 199 /** Prepare the ramdisk image for operation. */ 184 200 static bool rd_init(void) -
uspace/srv/fs/fat/fat_ops.c
rff62c6d r1ee00b7 818 818 819 819 /* prepare the boot block */ 820 rc = block_bb_read(dev_handle, BS_BLOCK * BS_SIZE, BS_SIZE);820 rc = block_bb_read(dev_handle, BS_BLOCK); 821 821 if (rc != EOK) { 822 822 block_fini(dev_handle); -
uspace/srv/fs/tmpfs/tmpfs_dump.c
rff62c6d r1ee00b7 47 47 #include <byteorder.h> 48 48 49 #define TMPFS_ BLOCK_SIZE102449 #define TMPFS_COMM_SIZE 1024 50 50 51 51 struct rdentry { … … 69 69 70 70 if (block_seqread(dev, bufpos, buflen, pos, &entry, 71 sizeof(entry) , TMPFS_BLOCK_SIZE) != EOK)71 sizeof(entry)) != EOK) 72 72 return false; 73 73 … … 89 89 90 90 if (block_seqread(dev, bufpos, buflen, pos, fname, 91 entry.len , TMPFS_BLOCK_SIZE) != EOK) {91 entry.len) != EOK) { 92 92 ops->destroy(fn); 93 93 free(fname); … … 105 105 106 106 if (block_seqread(dev, bufpos, buflen, pos, &size, 107 sizeof(size) , TMPFS_BLOCK_SIZE) != EOK)107 sizeof(size)) != EOK) 108 108 return false; 109 109 … … 117 117 nodep->size = size; 118 118 if (block_seqread(dev, bufpos, buflen, pos, nodep->data, 119 size , TMPFS_BLOCK_SIZE) != EOK)119 size) != EOK) 120 120 return false; 121 121 … … 133 133 134 134 if (block_seqread(dev, bufpos, buflen, pos, fname, 135 entry.len , TMPFS_BLOCK_SIZE) != EOK) {135 entry.len) != EOK) { 136 136 ops->destroy(fn); 137 137 free(fname); … … 166 166 int rc; 167 167 168 rc = block_init(dev, TMPFS_ BLOCK_SIZE);168 rc = block_init(dev, TMPFS_COMM_SIZE); 169 169 if (rc != EOK) 170 170 return false; … … 175 175 176 176 char tag[6]; 177 if (block_seqread(dev, &bufpos, &buflen, &pos, tag, 5, 178 TMPFS_BLOCK_SIZE) != EOK) 177 if (block_seqread(dev, &bufpos, &buflen, &pos, tag, 5) != EOK) 179 178 goto error; 180 179
Note:
See TracChangeset
for help on using the changeset viewer.