dev.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #if (CONFIG_COMMANDS & CFG_CMD_EXT2)
  27. #include <config.h>
  28. #include <ext2fs.h>
  29. static block_dev_desc_t *ext2fs_block_dev_desc;
  30. static disk_partition_t part_info;
  31. #undef DEBUG
  32. int ext2fs_set_blk_dev (block_dev_desc_t * rbdd, int part)
  33. {
  34. ext2fs_block_dev_desc = rbdd;
  35. if (part == 0) {
  36. /* disk doesn't use partition table */
  37. part_info.start = 0;
  38. part_info.size = rbdd->lba;
  39. part_info.blksz = rbdd->blksz;
  40. } else {
  41. if (get_partition_info
  42. (ext2fs_block_dev_desc, part, &part_info)) {
  43. return 0;
  44. }
  45. }
  46. return (part_info.size);
  47. }
  48. int ext2fs_devread (int sector, int byte_offset, int byte_len, char *buf) {
  49. char sec_buf[SECTOR_SIZE];
  50. unsigned block_len;
  51. /*
  52. * Check partition boundaries
  53. */
  54. if ((sector < 0)
  55. || ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >=
  56. part_info.size)) {
  57. /* errnum = ERR_OUTSIDE_PART; */
  58. printf (" ** ext2fs_devread() read outside partition sector %d\n", sector);
  59. return (0);
  60. }
  61. /*
  62. * Get the read to the beginning of a partition.
  63. */
  64. sector += byte_offset >> SECTOR_BITS;
  65. byte_offset &= SECTOR_SIZE - 1;
  66. #if defined(DEBUG)
  67. printf (" <%d, %d, %d>\n", sector, byte_offset, byte_len);
  68. #endif
  69. if (ext2fs_block_dev_desc == NULL) {
  70. printf ("** Invalid Block Device Descriptor (NULL)\n");
  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 (" ** ext2fs_devread() read error **\n");
  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. block_len = byte_len & ~(SECTOR_SIZE - 1);
  90. if (ext2fs_block_dev_desc->block_read (ext2fs_block_dev_desc->dev,
  91. part_info.start + sector,
  92. block_len / SECTOR_SIZE,
  93. (unsigned long *) buf) !=
  94. block_len / SECTOR_SIZE) {
  95. printf (" ** ext2fs_devread() read error - block\n");
  96. return (0);
  97. }
  98. buf += block_len;
  99. byte_len -= block_len;
  100. sector += block_len / SECTOR_SIZE;
  101. if (byte_len != 0) {
  102. /* read rest of data which are not in whole sector */
  103. if (ext2fs_block_dev_desc->
  104. block_read (ext2fs_block_dev_desc->dev,
  105. part_info.start + sector, 1,
  106. (unsigned long *) sec_buf) != 1) {
  107. printf (" ** ext2fs_devread() read error - last part\n");
  108. return (0);
  109. }
  110. memcpy (buf, sec_buf, byte_len);
  111. }
  112. return (1);
  113. }
  114. #endif /* CFG_CMD_EXT2FS */