dev.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. *
  3. * based on code of fs/reiserfs/dev.c by
  4. *
  5. * (C) Copyright 2003 - 2004
  6. * Sysgo AG, <www.elinos.com>, Pavel Bartusek <pba@sysgo.com>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <config.h>
  12. #include <zfs_common.h>
  13. static block_dev_desc_t *zfs_block_dev_desc;
  14. static disk_partition_t *part_info;
  15. void zfs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info)
  16. {
  17. zfs_block_dev_desc = rbdd;
  18. part_info = info;
  19. }
  20. /* err */
  21. int zfs_devread(int sector, int byte_offset, int byte_len, char *buf)
  22. {
  23. short sec_buffer[SECTOR_SIZE/sizeof(short)];
  24. char *sec_buf = (char *)sec_buffer;
  25. unsigned block_len;
  26. /*
  27. * Check partition boundaries
  28. */
  29. if ((sector < 0) ||
  30. ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >=
  31. part_info->size)) {
  32. /* errnum = ERR_OUTSIDE_PART; */
  33. printf(" ** zfs_devread() read outside partition sector %d\n", sector);
  34. return 1;
  35. }
  36. /*
  37. * Get the read to the beginning of a partition.
  38. */
  39. sector += byte_offset >> SECTOR_BITS;
  40. byte_offset &= SECTOR_SIZE - 1;
  41. debug(" <%d, %d, %d>\n", sector, byte_offset, byte_len);
  42. if (zfs_block_dev_desc == NULL) {
  43. printf("** Invalid Block Device Descriptor (NULL)\n");
  44. return 1;
  45. }
  46. if (byte_offset != 0) {
  47. /* read first part which isn't aligned with start of sector */
  48. if (zfs_block_dev_desc->block_read(zfs_block_dev_desc,
  49. part_info->start + sector, 1,
  50. (void *)sec_buf)
  51. != 1) {
  52. printf(" ** zfs_devread() read error **\n");
  53. return 1;
  54. }
  55. memcpy(buf, sec_buf + byte_offset,
  56. min(SECTOR_SIZE - byte_offset, byte_len));
  57. buf += min(SECTOR_SIZE - byte_offset, byte_len);
  58. byte_len -= min(SECTOR_SIZE - byte_offset, byte_len);
  59. sector++;
  60. }
  61. if (byte_len == 0)
  62. return 0;
  63. /* read sector aligned part */
  64. block_len = byte_len & ~(SECTOR_SIZE - 1);
  65. if (block_len == 0) {
  66. u8 p[SECTOR_SIZE];
  67. block_len = SECTOR_SIZE;
  68. zfs_block_dev_desc->block_read(zfs_block_dev_desc,
  69. part_info->start + sector,
  70. 1, (void *)p);
  71. memcpy(buf, p, byte_len);
  72. return 0;
  73. }
  74. if (zfs_block_dev_desc->block_read(zfs_block_dev_desc,
  75. part_info->start + sector,
  76. block_len / SECTOR_SIZE,
  77. (void *)buf)
  78. != block_len / SECTOR_SIZE) {
  79. printf(" ** zfs_devread() read error - block\n");
  80. return 1;
  81. }
  82. block_len = byte_len & ~(SECTOR_SIZE - 1);
  83. buf += block_len;
  84. byte_len -= block_len;
  85. sector += block_len / SECTOR_SIZE;
  86. if (byte_len != 0) {
  87. /* read rest of data which are not in whole sector */
  88. if (zfs_block_dev_desc->block_read(zfs_block_dev_desc,
  89. part_info->start + sector,
  90. 1, (void *)sec_buf) != 1) {
  91. printf(" ** zfs_devread() read error - last part\n");
  92. return 1;
  93. }
  94. memcpy(buf, sec_buf, byte_len);
  95. }
  96. return 0;
  97. }