dev.c 3.5 KB

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