dev.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * (C) Copyright 2004
  3. * esd gmbh <www.esd-electronics.com>
  4. * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
  5. *
  6. * based on code of fs/reiserfs/dev.c by
  7. *
  8. * (C) Copyright 2003 - 2004
  9. * Sysgo AG, <www.elinos.com>, Pavel Bartusek <pba@sysgo.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <common.h>
  26. #include <config.h>
  27. #include <ext2fs.h>
  28. static block_dev_desc_t *ext2fs_block_dev_desc;
  29. static disk_partition_t part_info;
  30. int ext2fs_set_blk_dev(block_dev_desc_t *rbdd, int part)
  31. {
  32. ext2fs_block_dev_desc = rbdd;
  33. if (part == 0) {
  34. /* disk doesn't use partition table */
  35. part_info.start = 0;
  36. part_info.size = rbdd->lba;
  37. part_info.blksz = rbdd->blksz;
  38. } else {
  39. if (get_partition_info
  40. (ext2fs_block_dev_desc, part, &part_info)) {
  41. return 0;
  42. }
  43. }
  44. return part_info.size;
  45. }
  46. int ext2fs_devread(int sector, int byte_offset, int byte_len, char *buf)
  47. {
  48. char sec_buf[SECTOR_SIZE];
  49. unsigned sectors;
  50. /*
  51. * Check partition boundaries
  52. */
  53. if ((sector < 0) ||
  54. ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >=
  55. part_info.size)) {
  56. /* errnum = ERR_OUTSIDE_PART; */
  57. printf(" ** %s read outside partition sector %d\n",
  58. __func__,
  59. sector);
  60. return 0;
  61. }
  62. /*
  63. * Get the read to the beginning of a partition.
  64. */
  65. sector += byte_offset >> SECTOR_BITS;
  66. byte_offset &= SECTOR_SIZE - 1;
  67. debug(" <%d, %d, %d>\n", sector, byte_offset, byte_len);
  68. if (ext2fs_block_dev_desc == NULL) {
  69. printf(" ** %s Invalid Block Device Descriptor (NULL)\n",
  70. __func__);
  71. return 0;
  72. }
  73. if (byte_offset != 0) {
  74. /* read first part which isn't aligned with start of sector */
  75. if (ext2fs_block_dev_desc->
  76. block_read(ext2fs_block_dev_desc->dev,
  77. part_info.start + sector, 1,
  78. (unsigned long *) sec_buf) != 1) {
  79. printf(" ** %s read error **\n", __func__);
  80. return 0;
  81. }
  82. memcpy(buf, sec_buf + byte_offset,
  83. min(SECTOR_SIZE - byte_offset, byte_len));
  84. buf += min(SECTOR_SIZE - byte_offset, byte_len);
  85. byte_len -= min(SECTOR_SIZE - byte_offset, byte_len);
  86. sector++;
  87. }
  88. /* read sector aligned part */
  89. sectors = byte_len / SECTOR_SIZE;
  90. if (sectors > 0) {
  91. if (ext2fs_block_dev_desc->block_read(
  92. ext2fs_block_dev_desc->dev,
  93. part_info.start + sector,
  94. sectors,
  95. (unsigned long *) buf) != sectors) {
  96. printf(" ** %s read error - block\n", __func__);
  97. return 0;
  98. }
  99. buf += sectors * SECTOR_SIZE;
  100. byte_len -= sectors * SECTOR_SIZE;
  101. sector += sectors;
  102. }
  103. if (byte_len != 0) {
  104. /* read rest of data which are not in whole sector */
  105. if (ext2fs_block_dev_desc->
  106. block_read(ext2fs_block_dev_desc->dev,
  107. part_info.start + sector, 1,
  108. (unsigned long *) sec_buf) != 1) {
  109. printf(" ** %s read error - last part\n", __func__);
  110. return 0;
  111. }
  112. memcpy(buf, sec_buf, byte_len);
  113. }
  114. return 1;
  115. }