@@ -39,13 +39,11 @@ int main(int argc, char *argv[])
3939{
4040 if ( argc != 3 )
4141 {
42- printf (" error - not enough arguments" );
43- return 1 ;
42+ return 1 ; // not enough arguments
4443 }
4544
4645 const char ltr = argv[2 ][0 ];
4746 const char *imagefile = argv[1 ];
48- printf (" writing file %s to device %c:" , imagefile, ltr);
4947
5048 status = STATUS_IDLE ;
5149 hVolume = INVALID_HANDLE_VALUE ;
@@ -58,45 +56,40 @@ int main(int argc, char *argv[])
5856 {
5957 if (INVALID_FILE_ATTRIBUTES == GetFileAttributes (imagefile) && GetLastError ()==ERROR_FILE_NOT_FOUND )
6058 {
61- printf (" \n error - file not found" );
62- return 1 ;
59+ return 2 ; // .img not found
6360 }
6461 else
6562 {
6663 status = STATUS_WRITING ;
6764
6865 unsigned long long i, availablesectors, numsectors;
6966 int volumeID = ltr - ' A' ;
70- int deviceID = getDriveNumberFromLetter (ltr);
67+ int deviceID = getDriveNumberFromLetter (ltr);
7168 if ( deviceID == -1 )
7269 {
73- printf (" \n error - device not found" );
74- return 1 ;
70+ return 3 ; // device not found
7571 }
7672
7773 hVolume = getHandleOnVolume (volumeID, GENERIC_WRITE );
7874 if (hVolume == INVALID_HANDLE_VALUE )
7975 {
8076 status = STATUS_IDLE ;
81- printf (" \n error - invalid handle value for volume" );
82- return 1 ;
77+ return 4 ; // invalid handle value for volume
8378 }
8479 if (!getLockOnVolume (hVolume))
8580 {
8681 CloseHandle (hVolume);
8782 status = STATUS_IDLE ;
8883 hVolume = INVALID_HANDLE_VALUE ;
89- printf (" \n error - can't get a lock on volume" );
90- return 1 ;
84+ return 5 ; // can't get lock on volume
9185 }
9286 if (!unmountVolume (hVolume))
9387 {
9488 removeLockOnVolume (hVolume);
9589 CloseHandle (hVolume);
9690 status = STATUS_IDLE ;
9791 hVolume = INVALID_HANDLE_VALUE ;
98- printf (" \n error - can't unmount volume" );
99- return 1 ;
92+ return 6 ; // can't unmount volume
10093 }
10194 hFile = getHandleOnFile (imagefile, GENERIC_READ );
10295 if (hFile == INVALID_HANDLE_VALUE )
@@ -105,8 +98,7 @@ int main(int argc, char *argv[])
10598 CloseHandle (hVolume);
10699 status = STATUS_IDLE ;
107100 hVolume = INVALID_HANDLE_VALUE ;
108- printf (" \n error - invalid handle value for file" );
109- return 1 ;
101+ return 7 ; // invalid handle value for file
110102 }
111103 hRawDisk = getHandleOnDevice (deviceID, GENERIC_WRITE );
112104 if (hRawDisk == INVALID_HANDLE_VALUE )
@@ -117,15 +109,13 @@ int main(int argc, char *argv[])
117109 status = STATUS_IDLE ;
118110 hVolume = INVALID_HANDLE_VALUE ;
119111 hFile = INVALID_HANDLE_VALUE ;
120- printf (" \n error - invalid handle value for disk" );
121- return 1 ;
112+ return 8 ; // invalid handle value for disk
122113 }
123114 availablesectors = getNumberOfSectors (hRawDisk, §orsize);
124115 numsectors = getFileSizeInSectors (hFile, sectorsize);
125116 if (numsectors > availablesectors)
126117 {
127- printf (" \n error - not enough space" );
128- return 1 ;
118+ return 9 ; // not enough space on volume
129119 }
130120
131121 char total_string[32 ];
@@ -154,8 +144,7 @@ int main(int argc, char *argv[])
154144 hRawDisk = INVALID_HANDLE_VALUE ;
155145 hFile = INVALID_HANDLE_VALUE ;
156146 hVolume = INVALID_HANDLE_VALUE ;
157- printf (" \n error - data is null" );
158- return 1 ;
147+ return 10 ; // sector data is null
159148 }
160149 if (!writeSectorDataToHandle (hRawDisk, sectorData, i, (numsectors - i >= 1024ul ) ? 1024ul :(numsectors - i), sectorsize))
161150 {
@@ -169,8 +158,7 @@ int main(int argc, char *argv[])
169158 hRawDisk = INVALID_HANDLE_VALUE ;
170159 hFile = INVALID_HANDLE_VALUE ;
171160 hVolume = INVALID_HANDLE_VALUE ;
172- printf (" \n error - error whilst writing" );
173- return 1 ;
161+ return 11 ; // error whilst writing
174162 }
175163 delete[] sectorData;
176164 sectorData = NULL ;
@@ -183,16 +171,14 @@ int main(int argc, char *argv[])
183171 hFile = INVALID_HANDLE_VALUE ;
184172 hVolume = INVALID_HANDLE_VALUE ;
185173 }
186- printf (" \n info - write successful" );
187- return 0 ;
174+ return 0 ; // success
188175 }
189176 else
190177 {
191- printf (" \n error - no image file specified" );
192- return 1 ;
178+ return 1 ; // not enough arguments
193179 }
194180 status = STATUS_IDLE ;
195- return 1 ;
181+ return 11 ; // error whilst writing
196182}
197183
198184int getDriveNumberFromLetter (char lookingforname)
@@ -230,7 +216,7 @@ HANDLE getHandleOnFile(const char *filelocation, DWORD access)
230216 hFile = CreateFileA (filelocation, access, (access == GENERIC_READ ) ? FILE_SHARE_READ : 0 , NULL , (access == GENERIC_READ ) ? OPEN_EXISTING :CREATE_ALWAYS , 0 , NULL );
231217 if (hFile == INVALID_HANDLE_VALUE )
232218 {
233- printf (" \n error - not able to get handle on image file" );
219+ // printf("error - not able to get handle on image file");
234220 }
235221 return hFile;
236222}
@@ -244,7 +230,7 @@ HANDLE getHandleOnDevice(int device, DWORD access)
244230 hDevice = CreateFile (devicename, access, FILE_SHARE_READ | FILE_SHARE_WRITE , NULL , OPEN_EXISTING , 0 , NULL );
245231 if (hDevice == INVALID_HANDLE_VALUE )
246232 {
247- printf (" \n error - not able to get handle on device" );
233+ // printf("error - not able to get handle on device");
248234 }
249235 return hDevice;
250236}
@@ -258,7 +244,7 @@ HANDLE getHandleOnVolume(int volume, DWORD access)
258244 hVolume = CreateFile (volumename, access, FILE_SHARE_READ | FILE_SHARE_WRITE , NULL , OPEN_EXISTING , 0 , NULL );
259245 if (hVolume == INVALID_HANDLE_VALUE )
260246 {
261- printf (" \n error - not able to get handle on volume" );
247+ // printf("error - not able to get handle on volume");
262248 }
263249 return hVolume;
264250}
@@ -270,7 +256,7 @@ bool getLockOnVolume(HANDLE handle)
270256 bResult = DeviceIoControl (handle, FSCTL_LOCK_VOLUME , NULL , 0 , NULL , 0 , &bytesreturned, NULL );
271257 if (!bResult)
272258 {
273- printf (" \n error - not able to lock volume" );
259+ // printf("error - not able to lock volume");
274260 }
275261 return (bResult);
276262}
@@ -282,7 +268,7 @@ bool removeLockOnVolume(HANDLE handle)
282268 bResult = DeviceIoControl (handle, FSCTL_UNLOCK_VOLUME , NULL , 0 , NULL , 0 , &junk, NULL );
283269 if (!bResult)
284270 {
285- printf (" \n error - not able to unlock volume" );
271+ // printf("error - not able to unlock volume");
286272 }
287273 return (bResult);
288274}
@@ -294,7 +280,7 @@ bool unmountVolume(HANDLE handle)
294280 bResult = DeviceIoControl (handle, FSCTL_DISMOUNT_VOLUME , NULL , 0 , NULL , 0 , &junk, NULL );
295281 if (!bResult)
296282 {
297- printf (" \n error - not able to dismount volume" );
283+ // printf("error - not able to dismount volume");
298284 }
299285 return (bResult);
300286}
@@ -308,7 +294,7 @@ char *readSectorDataFromHandle(HANDLE handle, unsigned long long startsector, un
308294 SetFilePointer (handle, li.LowPart , &li.HighPart , FILE_BEGIN );
309295 if (!ReadFile (handle, data, sectorsize * numsectors, &bytesread, NULL ))
310296 {
311- printf (" \n error - not able to read data from handle" );
297+ // printf("error - not able to read data from handle");
312298 delete[] data;
313299 data = NULL ;
314300 }
@@ -329,7 +315,7 @@ bool writeSectorDataToHandle(HANDLE handle, char *data, unsigned long long start
329315 bResult = WriteFile (handle, data, sectorsize * numsectors, &byteswritten, NULL );
330316 if (!bResult)
331317 {
332- printf (" \n error - not able to write data from handle" );
318+ // printf("error - not able to write data from handle");
333319 }
334320 return (bResult);
335321}
@@ -342,8 +328,7 @@ unsigned long long getNumberOfSectors(HANDLE handle, unsigned long long *sectors
342328 bResult = DeviceIoControl (handle, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX , NULL , 0 , &diskgeometry, sizeof (diskgeometry), &junk, NULL );
343329 if (!bResult)
344330 {
345- printf (" \n error - not able to get number of sectors" );
346- return 0 ;
331+ return 12 ;
347332 }
348333 if (sectorsize != NULL )
349334 {
@@ -358,7 +343,7 @@ unsigned long long getFileSizeInSectors(HANDLE handle, unsigned long long sector
358343 LARGE_INTEGER filesize;
359344 if (GetFileSizeEx (handle, &filesize) == 0 )
360345 {
361- printf (" \n error - not able to get image file size" );
346+ // printf("error - not able to get image file size");
362347 retVal = 0 ;
363348 }
364349 else
@@ -393,15 +378,17 @@ BOOL GetDisksProperty(HANDLE hDevice, PSTORAGE_DEVICE_DESCRIPTOR pDevDesc,
393378 if (!bResult)
394379 {
395380 retVal = false ;
396- printf (" \n error - not able to get device number, is something accessing the device?" );
381+ // printf("error - not able to get device number, is something accessing the device?");
382+ // setbuf(stdout, NULL);
397383 }
398384 }
399385 else
400386 {
401387 if (DeviceIoControl (hDevice, IOCTL_STORAGE_CHECK_VERIFY2 , NULL , 0 , NULL , 0 , &cbBytesReturned,
402388 (LPOVERLAPPED ) NULL ))
403389 {
404- printf (" \n error - not able to get device properties, is something accessing the device?" );
390+ // printf("error - not able to get device properties, is something accessing the device?");
391+ // setbuf(stdout, NULL);
405392 }
406393 retVal = false ;
407394 }
@@ -466,7 +453,8 @@ bool checkDriveType(char *name, ULONG *pid)
466453 hDevice = CreateFile (nameNoSlash, FILE_READ_ATTRIBUTES , FILE_SHARE_READ | FILE_SHARE_WRITE , NULL , OPEN_EXISTING , 0 , NULL );
467454 if (hDevice == INVALID_HANDLE_VALUE )
468455 {
469- printf (" \n error - not able to get handle on device" );
456+ // printf("error - not able to get handle on device");
457+ // setbuf(stdout, NULL);
470458 }
471459 else
472460 {
0 commit comments