lwext4_mbr.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * - The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <getopt.h>
  33. #include <stdbool.h>
  34. #include <inttypes.h>
  35. #include <time.h>
  36. #include <unistd.h>
  37. #include <sys/time.h>
  38. #include <ext4.h>
  39. #include <ext4_mbr.h>
  40. #include "../blockdev/linux/file_dev.h"
  41. #include "../blockdev/windows/file_windows.h"
  42. /**@brief Input stream name.*/
  43. const char *input_name = NULL;
  44. /**@brief Block device handle.*/
  45. static struct ext4_blockdev *bd;
  46. /**@brief Indicates that input is windows partition.*/
  47. static bool winpart = false;
  48. static bool verbose = false;
  49. static const char *usage = " \n\
  50. Welcome in lwext4_mbr tool. \n\
  51. Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com) \n\
  52. Usage: \n\
  53. [-i] --input - input file name (or blockdevice) \n\
  54. [-w] --wpart - windows partition mode \n\
  55. [-v] --verbose - verbose mode \n\
  56. \n";
  57. static bool open_linux(void)
  58. {
  59. file_dev_name_set(input_name);
  60. bd = file_dev_get();
  61. if (!bd) {
  62. printf("open_filedev: fail\n");
  63. return false;
  64. }
  65. return true;
  66. }
  67. static bool open_windows(void)
  68. {
  69. #ifdef WIN32
  70. file_windows_name_set(input_name);
  71. bd = file_windows_dev_get();
  72. if (!bd) {
  73. printf("open_winpartition: fail\n");
  74. return false;
  75. }
  76. return true;
  77. #else
  78. printf("open_winpartition: this mode should be used only under windows "
  79. "!\n");
  80. return false;
  81. #endif
  82. }
  83. static bool open_filedev(void)
  84. {
  85. return winpart ? open_windows() : open_linux();
  86. }
  87. static bool parse_opt(int argc, char **argv)
  88. {
  89. int option_index = 0;
  90. int c;
  91. static struct option long_options[] = {
  92. {"input", required_argument, 0, 'i'},
  93. {"wpart", no_argument, 0, 'w'},
  94. {"verbose", no_argument, 0, 'v'},
  95. {"version", no_argument, 0, 'x'},
  96. {0, 0, 0, 0}};
  97. while (-1 != (c = getopt_long(argc, argv, "i:wvx",
  98. long_options, &option_index))) {
  99. switch (c) {
  100. case 'i':
  101. input_name = optarg;
  102. break;
  103. case 'w':
  104. winpart = true;
  105. break;
  106. case 'v':
  107. verbose = true;
  108. break;
  109. case 'x':
  110. puts(VERSION);
  111. exit(0);
  112. break;
  113. default:
  114. printf("%s", usage);
  115. return false;
  116. }
  117. }
  118. return true;
  119. }
  120. int main(int argc, char **argv)
  121. {
  122. int r;
  123. if (!parse_opt(argc, argv)){
  124. printf("parse_opt error\n");
  125. return EXIT_FAILURE;
  126. }
  127. if (!open_filedev()) {
  128. printf("open_filedev error\n");
  129. return EXIT_FAILURE;
  130. }
  131. if (verbose)
  132. ext4_dmask_set(DEBUG_ALL);
  133. printf("ext4_mbr\n");
  134. struct ext4_mbr_bdevs bdevs;
  135. r = ext4_mbr_scan(bd, &bdevs);
  136. if (r != EOK) {
  137. printf("ext4_mbr_scan error\n");
  138. return EXIT_FAILURE;
  139. }
  140. int i;
  141. printf("ext4_mbr_scan:\n");
  142. for (i = 0; i < 4; i++) {
  143. printf("mbr_entry %d:\n", i);
  144. if (!bdevs.partitions[i].bdif) {
  145. printf("\tempty/unknown\n");
  146. continue;
  147. }
  148. printf("\toffeset: 0x%"PRIx64", %"PRIu64"MB\n",
  149. bdevs.partitions[i].part_offset,
  150. bdevs.partitions[i].part_offset / (1024 * 1024));
  151. printf("\tsize: 0x%"PRIx64", %"PRIu64"MB\n",
  152. bdevs.partitions[i].part_size,
  153. bdevs.partitions[i].part_size / (1024 * 1024));
  154. }
  155. return EXIT_SUCCESS;
  156. }