dev.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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
  33. (
  34. block_dev_desc_t *rbdd,
  35. int part
  36. )
  37. {
  38. ext2fs_block_dev_desc = rbdd;
  39. if (part == 0)
  40. {
  41. /* disk doesn't use partition table */
  42. part_info.start = 0;
  43. part_info.size = rbdd->lba;
  44. part_info.blksz = rbdd->blksz;
  45. }
  46. else
  47. {
  48. if (get_partition_info (ext2fs_block_dev_desc, part, &part_info))
  49. {
  50. return 0;
  51. }
  52. }
  53. return (part_info.size);
  54. }
  55. int ext2fs_devread
  56. (
  57. int sector,
  58. int byte_offset,
  59. int byte_len,
  60. char *buf
  61. )
  62. {
  63. char sec_buf[SECTOR_SIZE];
  64. unsigned block_len;
  65. /*
  66. * Check partition boundaries
  67. */
  68. if ((sector < 0) || ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >= part_info.size))
  69. {
  70. /* errnum = ERR_OUTSIDE_PART; */
  71. printf (" ** ext2fs_devread() read outside partition sector %d\n", sector);
  72. return(0);
  73. }
  74. /*
  75. * Get the read to the beginning of a partition.
  76. */
  77. sector += byte_offset >> SECTOR_BITS;
  78. byte_offset &= SECTOR_SIZE - 1;
  79. #if defined(DEBUG)
  80. printf (" <%d, %d, %d>\n", sector, byte_offset, byte_len);
  81. #endif
  82. if (ext2fs_block_dev_desc == NULL)
  83. {
  84. printf("** Invalid Block Device Descriptor (NULL)\n");
  85. return(0);
  86. }
  87. if (byte_offset != 0)
  88. {
  89. /* read first part which isn't aligned with start of sector */
  90. if (ext2fs_block_dev_desc->block_read(ext2fs_block_dev_desc->dev, part_info.start+sector, 1, (unsigned long *)sec_buf) != 1)
  91. {
  92. printf (" ** ext2fs_devread() read error **\n");
  93. return(0);
  94. }
  95. memcpy(buf, sec_buf+byte_offset, min(SECTOR_SIZE-byte_offset, byte_len));
  96. buf+=min(SECTOR_SIZE-byte_offset, byte_len);
  97. byte_len-=min(SECTOR_SIZE-byte_offset, byte_len);
  98. sector++;
  99. }
  100. /* read sector aligned part */
  101. block_len = byte_len & ~(SECTOR_SIZE-1);
  102. if (ext2fs_block_dev_desc->block_read(ext2fs_block_dev_desc->dev,
  103. part_info.start+sector,
  104. block_len/SECTOR_SIZE,
  105. (unsigned long *)buf) != block_len/SECTOR_SIZE)
  106. {
  107. printf (" ** ext2fs_devread() read error - block\n");
  108. return(0);
  109. }
  110. buf+=block_len;
  111. byte_len-=block_len;
  112. sector+= block_len/SECTOR_SIZE;
  113. if (byte_len != 0)
  114. {
  115. /* read rest of data which are not in whole sector */
  116. if (ext2fs_block_dev_desc->block_read(ext2fs_block_dev_desc->dev,
  117. part_info.start+sector,
  118. 1,
  119. (unsigned long *)sec_buf) != 1)
  120. {
  121. printf (" ** ext2fs_devread() read error - last part\n");
  122. return(0);
  123. }
  124. memcpy(buf, sec_buf, byte_len);
  125. }
  126. return(1);
  127. }
  128. #endif /* CFG_CMD_EXT2FS */