dev.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * (C) Copyright 2003 - 2004
  3. * Sysgo AG, <www.elinos.com>, Pavel Bartusek <pba@sysgo.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <config.h>
  9. #include <reiserfs.h>
  10. #include "reiserfs_private.h"
  11. static block_dev_desc_t *reiserfs_block_dev_desc;
  12. static disk_partition_t *part_info;
  13. void reiserfs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info)
  14. {
  15. reiserfs_block_dev_desc = rbdd;
  16. part_info = info;
  17. }
  18. int reiserfs_devread (int sector, int byte_offset, int byte_len, char *buf)
  19. {
  20. char sec_buf[SECTOR_SIZE];
  21. unsigned block_len;
  22. /*
  23. unsigned len = byte_len;
  24. u8 *start = buf;
  25. */
  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 (" ** reiserfs_devread() read outside partition\n");
  34. return 0;
  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. #if defined(DEBUG)
  42. printf (" <%d, %d, %d> ", sector, byte_offset, byte_len);
  43. #endif
  44. if (reiserfs_block_dev_desc == NULL)
  45. return 0;
  46. if (byte_offset != 0) {
  47. /* read first part which isn't aligned with start of sector */
  48. if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc,
  49. part_info->start +
  50. sector,
  51. 1, (void *)sec_buf)
  52. != 1) {
  53. printf (" ** reiserfs_devread() read error\n");
  54. return 0;
  55. }
  56. memcpy(buf, sec_buf+byte_offset, 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. /* read sector aligned part */
  62. block_len = byte_len & ~(SECTOR_SIZE-1);
  63. if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc,
  64. part_info->start + sector,
  65. block_len / SECTOR_SIZE,
  66. (void *)buf)
  67. != block_len/SECTOR_SIZE) {
  68. printf (" ** reiserfs_devread() read error - block\n");
  69. return 0;
  70. }
  71. buf+=block_len;
  72. byte_len-=block_len;
  73. sector+= block_len/SECTOR_SIZE;
  74. if ( byte_len != 0 ) {
  75. /* read rest of data which are not in whole sector */
  76. if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc,
  77. part_info->start +
  78. sector,
  79. 1, (void *)sec_buf)
  80. != 1) {
  81. printf (" ** reiserfs_devread() read error - last part\n");
  82. return 0;
  83. }
  84. memcpy(buf, sec_buf, byte_len);
  85. }
  86. return 1;
  87. }