Write a test program to see which sectors in the disk image file can be used?

Source: Internet
Author: User

Do the boot loader experiment, but use it on a USB drive formatted as FAT32.

* Use Winhex to fill all the bytes of the U disk into 0.

* Format the USB stick into FAT32 with the format function in UltraISO, the allocation unit is 4KB

* Use the Write MBR function provided by UltraISO to write the bootable MBR to the USB drive with the parameter usb-hdd.

This problem comes, MBR in 0 sectors, we can write the MBR by reverse ultraiso, rewrite MBR + USB stick formatted well after the partition table (16 * 4 bytes), form its own MBR.

But we can't change the other sectors that are used. Otherwise, the partition information is destroyed, after the USB stick is inserted, Win7 prompts "This USB flash drive is not formatted, is now formatted."

Once in a row, we can click to turn off this hint. It's a long time to do the experiment and I can't stand the hint.

There are 2 ways to solve this problem:

* Study partition table information, make a self-starter USB flash drive like old Peaches. But my goal is only to do the boot loader experiment, now the time and ability are not allowed to do so

* Find unused sectors (all 0 sectors), after our own MBR execution completes, jump directly to our custom first sector (also not used by the Windows partition to determine the use of sectors), by this sector to load we do the experiment of the subsequent sectors to memory. This method is highly operable and can avoid annoying hints.

In the end can be done in the experiment of the U disk INSERT, let Win7 not pop-up hints, I have not experimented.

Now write a test program to find out exactly those sectors are used, the number of sectors used, using Winhex manual view, the number is still relatively small.

Afraid to see the blurred, with the test program to find.

Test program Download point: Src_find_used_sectors.zip

:

Project preview:

[CPP]View Plaincopy
  1. @file Find_used_sectors.cpp
  2. @brief Find out if the FAT32 file system uses a sector to do the boot loader experiment
  3. Prevent u disk after the experiment, insert a USB flash drive, was Win7 prompted "This USB flash drive is not formatted, is now formatted"
  4. Every time you insert a USB flash drive, you are prompted with information, annoying
  5. #include "stdafx.h"
  6. First use Winhex to fill the USB stick 0
  7. The USB flash drive in ultraios format, parameters: FAT32, 4KB
  8. Write mbr, parameter: USB-HDD
  9. Copy the contents of the USB drive to a file with Winhex
  10. Using this test program to find those sectors is used (there are not 0 bytes)
  11. When we do a U disk experiment, we can determine in which sector write things, will not let Win7 pop "USB stick Invalid" prompt
  12. #define Obj_file_to_parse L "D:\\udiskbk\\udisk_format_ok_write_mbr_by_ultra_iso.bin"
  13. #define SECTOR_SIZE 512
  14. BOOL Findandshownozerocontentsectorsindex (handle& hfile, longlong llfilesize); ///< Lookup shows sectors with non-0 content
  15. BOOL judgesectorwasused (byte* pcsectorbegin, int ilensector);
  16. int _tmain (int argc, _tchar* argv[])
  17. {
  18. HANDLE hfile = Invalid_handle_value;
  19. Longlong llfilesize = 0;
  20. Do
  21. {
  22. if (!ns_base::isfileexist (obj_file_to_parse))
  23. {
  24. _tprintf (L"File not exist[%s]\r\n", obj_file_to_parse);
  25. Break ;
  26. }
  27. Llfilesize = Ns_base::getfilesize (obj_file_to_parse);
  28. if (llfilesize < 0)
  29. {
  30. _tprintf (L"File size [0x%i64x] invalid\r\n", llfilesize);
  31. Break ;
  32. }
  33. if (0! = (llfilesize%))
  34. {
  35. _tprintf (L"File can ' t mod by sector size (bytes\r\n");
  36. }
  37. if (!ns_base::openfilereadbinary (Obj_file_to_parse, hfile))
  38. Break ;
  39. if (! Findandshownozerocontentsectorsindex (hfile, llfilesize))
  40. {
  41. _tprintf (L"FALSE findandshownozerocontentsectorsindex () \ r \ n");
  42. }
  43. Safe_close_handle (hfile);
  44. } while (0);
  45. _tprintf (L"END, press any key to quit\r\n");
  46. #ifdef _DEBUG
  47. Getwchar ();
  48. #endif
  49. Getwchar ();
  50. return 0;
  51. }
  52. BOOL Findandshownozerocontentsectorsindex (handle& hfile, longlong llfilesize)
  53. {
  54. BOOL bRc = FALSE;
  55. BYTE Cbufrd[sector_size];
  56. DWORD dwrdback = 0;
  57. long lsectorindexcur = 0;
  58. long lsectorwasused = 0;
  59. Longlong llfilesizerd = 0;
  60. Longlong llposbegin = 0;
  61. Longlong llposend = 0;
  62. Do
  63. {
  64. if (Invalid_set_file_pointer = = SetFilePointer (hfile, 0, 0, file_begin))
  65. Break ;
  66. Do
  67. {
  68. :: ZeroMemory (CBUFRD, sizeof (CBUFRD));  ///<!
  69. if (!::readfile (hfile, &CBUFRD, sizeof (CBUFRD), &dwrdback, NULL))
  70. Break ;
  71. if (dwrdback! = sizeof (CBUFRD))
  72. Break ;
  73. if (judgesectorwasused (CBUFRD, sizeof (CBUFRD)))
  74. {
  75. lsectorwasused++;
  76. Llposbegin = (longlong) lsectorindexcur * sector_size;
  77. Llposend = llposbegin + sizeof (CBUFRD)-1;
  78. _tprintf (L"sector[0x%.8i 64x-0x%.8i 64X] Sector index[%ld] was used\r\n ",
  79. Llposbegin,
  80. Llposend,
  81. Lsectorindexcur);
  82. }
  83. Llfilesizerd + = Dwrdback;
  84. if (llfilesizerd = = llfilesize)
  85. {
  86. BRc = TRUE;
  87. Break ;
  88. }
  89. lsectorindexcur++;
  90. } while (1);
  91. } while (0);
  92. if (bRc)
  93. {
  94. _tprintf (L"lsectorwasused =%ld\r\n", lsectorwasused);
  95. }
  96. return bRc;
  97. }
  98. BOOL judgesectorwasused (byte* pcsectorbegin, int ilensector)
  99. {
  100. BOOL bRc = FALSE;
  101. int iIndex = 0;
  102. Do
  103. {
  104. if ((NULL = = Pcsectorbegin)
  105. || (sector_size! = ilensector))
  106. {
  107. Break ;
  108. }
  109. For (iIndex = 0; IIndex < sector_size; iindex++)
  110. {
  111. if (0! = * (Pcsectorbegin + iIndex))
  112. {
  113. BRc = TRUE;
  114. Break ;
  115. }
  116. }
  117. } while (0);
  118. return bRc;
  119. }

http://blog.csdn.net/lostspeed/article/details/48804455

http://download.csdn.net/detail/lostspeed/9146699

Write a test program to see which sectors in the disk image file can be used?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.