dev.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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->dev,
  49. part_info->start + sector, 1,
  50. (unsigned long *)sec_buf) != 1) {
  51. printf(" ** zfs_devread() read error **\n");
  52. return 1;
  53. }
  54. memcpy(buf, sec_buf + byte_offset,
  55. min(SECTOR_SIZE - byte_offset, byte_len));
  56. buf += min(SECTOR_SIZE - byte_offset, byte_len);
  57. byte_len -= min(SECTOR_SIZE - byte_offset, byte_len);
  58. sector++;
  59. }
  60. if (byte_len == 0)
  61. return 0;
  62. /* read sector aligned part */
  63. block_len = byte_len & ~(SECTOR_SIZE - 1);
  64. if (block_len == 0) {
  65. u8 p[SECTOR_SIZE];
  66. block_len = SECTOR_SIZE;
  67. zfs_block_dev_desc->block_read(zfs_block_dev_desc->dev,
  68. part_info->start + sector,
  69. 1, (unsigned long *)p);
  70. memcpy(buf, p, byte_len);
  71. return 0;
  72. }
  73. if (zfs_block_dev_desc->block_read(zfs_block_dev_desc->dev,
  74. part_info->start + sector, block_len / SECTOR_SIZE,
  75. (unsigned long *) buf) != block_len / SECTOR_SIZE) {
  76. printf(" ** zfs_devread() read error - block\n");
  77. return 1;
  78. }
  79. block_len = byte_len & ~(SECTOR_SIZE - 1);
  80. buf += block_len;
  81. byte_len -= block_len;
  82. sector += block_len / SECTOR_SIZE;
  83. if (byte_len != 0) {
  84. /* read rest of data which are not in whole sector */
  85. if (zfs_block_dev_desc->
  86. block_read(zfs_block_dev_desc->dev,
  87. part_info->start + sector, 1,
  88. (unsigned long *) sec_buf) != 1) {
  89. printf(" ** zfs_devread() read error - last part\n");
  90. return 1;
  91. }
  92. memcpy(buf, sec_buf, byte_len);
  93. }
  94. return 0;
  95. }