dev.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. char sec_buf[SECTOR_SIZE];
  48. unsigned block_len;
  49. /*
  50. * Check partition boundaries
  51. */
  52. if ((sector < 0)
  53. || ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >=
  54. part_info.size)) {
  55. /* errnum = ERR_OUTSIDE_PART; */
  56. printf (" ** ext2fs_devread() read outside partition sector %d\n", sector);
  57. return (0);
  58. }
  59. /*
  60. * Get the read to the beginning of a partition.
  61. */
  62. sector += byte_offset >> SECTOR_BITS;
  63. byte_offset &= SECTOR_SIZE - 1;
  64. debug (" <%d, %d, %d>\n", sector, byte_offset, byte_len);
  65. if (ext2fs_block_dev_desc == NULL) {
  66. printf ("** Invalid Block Device Descriptor (NULL)\n");
  67. return (0);
  68. }
  69. if (byte_offset != 0) {
  70. /* read first part which isn't aligned with start of sector */
  71. if (ext2fs_block_dev_desc->
  72. block_read (ext2fs_block_dev_desc->dev,
  73. part_info.start + sector, 1,
  74. (unsigned long *) sec_buf) != 1) {
  75. printf (" ** ext2fs_devread() read error **\n");
  76. return (0);
  77. }
  78. memcpy (buf, sec_buf + byte_offset,
  79. min (SECTOR_SIZE - byte_offset, byte_len));
  80. buf += min (SECTOR_SIZE - byte_offset, byte_len);
  81. byte_len -= min (SECTOR_SIZE - byte_offset, byte_len);
  82. sector++;
  83. }
  84. if (byte_len == 0)
  85. return 1;
  86. /* read sector aligned part */
  87. block_len = byte_len & ~(SECTOR_SIZE - 1);
  88. if (block_len == 0) {
  89. u8 p[SECTOR_SIZE];
  90. block_len = SECTOR_SIZE;
  91. ext2fs_block_dev_desc->block_read(ext2fs_block_dev_desc->dev,
  92. part_info.start + sector,
  93. 1, (unsigned long *)p);
  94. memcpy(buf, p, byte_len);
  95. return 1;
  96. }
  97. if (ext2fs_block_dev_desc->block_read (ext2fs_block_dev_desc->dev,
  98. part_info.start + sector,
  99. block_len / SECTOR_SIZE,
  100. (unsigned long *) buf) !=
  101. block_len / SECTOR_SIZE) {
  102. printf (" ** ext2fs_devread() read error - block\n");
  103. return (0);
  104. }
  105. block_len = byte_len & ~(SECTOR_SIZE - 1);
  106. buf += block_len;
  107. byte_len -= block_len;
  108. sector += block_len / SECTOR_SIZE;
  109. if (byte_len != 0) {
  110. /* read rest of data which are not in whole sector */
  111. if (ext2fs_block_dev_desc->
  112. block_read (ext2fs_block_dev_desc->dev,
  113. part_info.start + sector, 1,
  114. (unsigned long *) sec_buf) != 1) {
  115. printf (" ** ext2fs_devread() read error - last part\n");
  116. return (0);
  117. }
  118. memcpy (buf, sec_buf, byte_len);
  119. }
  120. return (1);
  121. }