Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 245 lines (218 sloc) 7.178 kb
bc25240 @pbatard [misc] move I/O functions into their own source
authored
1 /*
7726071 @pbatard [misc] updated acronym
authored
2 * Rufus: The Reliable USB Formatting Utility
bc25240 @pbatard [misc] move I/O functions into their own source
authored
3 * Standard I/O Routines (logging, status, etc.)
3187f71 @pbatard v1.0.6 (#109)
authored
4 * Copyright (c) 2011-2012 Pete Batard <pete@akeo.ie>
bc25240 @pbatard [misc] move I/O functions into their own source
authored
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
79f3e78 @pbatard misc] move formatting functions into their own source
authored
19 #ifdef _CRTDBG_MAP_ALLOC
20 #include <stdlib.h>
21 #include <crtdbg.h>
22 #endif
bc25240 @pbatard [misc] move I/O functions into their own source
authored
23
24 #include <windows.h>
fd55a4e @pbatard [ui] add log window
authored
25 #include <windowsx.h>
bc25240 @pbatard [misc] move I/O functions into their own source
authored
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <ctype.h>
30
31 #include "msapi_utf8.h"
32 #include "rufus.h"
33 #include "resource.h"
34
2390b30 @pbatard [misc] move drive related functions into their own source
authored
35 /*
36 * Globals
37 */
38 HWND hStatus;
39
bc25240 @pbatard [misc] move I/O functions into their own source
authored
40 #ifdef RUFUS_DEBUG
41 void _uprintf(const char *format, ...)
42 {
43 char buf[4096], *p = buf;
44 va_list args;
45 int n;
46
47 va_start(args, format);
48 n = safe_vsnprintf(p, sizeof(buf)-3, format, args); // buf-3 is room for CR/LF/NUL
49 va_end(args);
50
51 p += (n < 0)?sizeof(buf)-3:n;
52
7e7c751 @pbatard [ui] enable setting of icon and extended label
authored
53 while((p>buf) && (isspace((unsigned char)p[-1])))
bc25240 @pbatard [misc] move I/O functions into their own source
authored
54 *--p = '\0';
55
56 *p++ = '\r';
57 *p++ = '\n';
58 *p = '\0';
59
fd55a4e @pbatard [ui] add log window
authored
60 // Send output to Windows debug facility
bc25240 @pbatard [misc] move I/O functions into their own source
authored
61 OutputDebugStringA(buf);
fd55a4e @pbatard [ui] add log window
authored
62 // Send output to our log Window
63 Edit_SetSel(hLog, MAX_LOG_SIZE, MAX_LOG_SIZE);
64 Edit_ReplaceSelU(hLog, buf);
bc25240 @pbatard [misc] move I/O functions into their own source
authored
65 }
66 #endif
67
68 void DumpBufferHex(void *buf, size_t size)
69 {
70 unsigned char* buffer = (unsigned char*)buf;
71 size_t i, j, k;
72 char line[80] = "";
73
74 for (i=0; i<size; i+=16) {
75 if (i!=0)
76 uprintf("%s\n", line);
77 line[0] = 0;
78 sprintf(&line[strlen(line)], " %08x ", (unsigned int)i);
79 for(j=0,k=0; k<16; j++,k++) {
80 if (i+j < size) {
81 sprintf(&line[strlen(line)], "%02x", buffer[i+j]);
82 } else {
83 sprintf(&line[strlen(line)], " ");
84 }
85 sprintf(&line[strlen(line)], " ");
86 }
87 sprintf(&line[strlen(line)], " ");
88 for(j=0,k=0; k<16; j++,k++) {
89 if (i+j < size) {
90 if ((buffer[i+j] < 32) || (buffer[i+j] > 126)) {
91 sprintf(&line[strlen(line)], ".");
92 } else {
93 sprintf(&line[strlen(line)], "%c", buffer[i+j]);
94 }
95 }
96 }
97 }
98 uprintf("%s\n", line);
99 }
100
101 /*
102 * Convert a windows error to human readable string
103 * uses retval as errorcode, or, if 0, use GetLastError()
104 */
5065c02 @pbatard [ui] report errors to the user
authored
105 const char *WindowsErrorString(void)
bc25240 @pbatard [misc] move I/O functions into their own source
authored
106 {
107 static char err_string[256];
108
109 DWORD size;
110 DWORD error_code, format_error;
111
112 error_code = GetLastError();
113
5065c02 @pbatard [ui] report errors to the user
authored
114 safe_sprintf(err_string, sizeof(err_string), "[0x%08X] ", error_code);
bc25240 @pbatard [misc] move I/O functions into their own source
authored
115
7426573 @pbatard [iso] add download & replace of obsolete vesamenu.c32
authored
116 size = FormatMessageU(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error_code,
bc25240 @pbatard [misc] move I/O functions into their own source
authored
117 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &err_string[strlen(err_string)],
118 sizeof(err_string)-(DWORD)strlen(err_string), NULL);
119 if (size == 0) {
120 format_error = GetLastError();
5065c02 @pbatard [ui] report errors to the user
authored
121 if ((format_error) && (format_error != 0x13D)) // 0x13D, decode error, is returned for unknown codes
bc25240 @pbatard [misc] move I/O functions into their own source
authored
122 safe_sprintf(err_string, sizeof(err_string),
5065c02 @pbatard [ui] report errors to the user
authored
123 "Windows error code 0x%08X (FormatMessage error code 0x%08X)", error_code, format_error);
bc25240 @pbatard [misc] move I/O functions into their own source
authored
124 else
5065c02 @pbatard [ui] report errors to the user
authored
125 safe_sprintf(err_string, sizeof(err_string), "Unknown error 0x%08X", error_code);
bc25240 @pbatard [misc] move I/O functions into their own source
authored
126 }
127 return err_string;
128 }
129
b006e6a @pbatard [ui] better bad blocks reports
authored
130 /*
131 * Display a message on the status bar. If duration is non zero, ensures that message
132 * is displayed for at least duration ms regardless, regardless of any other incoming
133 * message
134 */
135 static BOOL bStatusTimerArmed = FALSE;
f4ed6e4 @pbatard [misc] fix broken DOS, fix invalid labels and UI improvements
authored
136 char szStatusMessage[256] = { 0 };
b006e6a @pbatard [ui] better bad blocks reports
authored
137 static void CALLBACK PrintStatusTimeout(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
138 {
139 bStatusTimerArmed = FALSE;
140 // potentially display lower priority message that was overridden
f4ed6e4 @pbatard [misc] fix broken DOS, fix invalid labels and UI improvements
authored
141 SendMessageLU(GetDlgItem(hMainDialog, IDC_STATUS), SB_SETTEXTW,
142 SBT_OWNERDRAW | 0, szStatusMessage);
17132c3 @pbatard [ui] progress bar improvements
authored
143 KillTimer(hMainDialog, TID_MESSAGE);
b006e6a @pbatard [ui] better bad blocks reports
authored
144 }
145
d1e4ba2 @pbatard [ntfs] add bootable NTFS support
authored
146 void PrintStatus(unsigned int duration, BOOL debug, const char *format, ...)
bc25240 @pbatard [misc] move I/O functions into their own source
authored
147 {
b006e6a @pbatard [ui] better bad blocks reports
authored
148 char *p = szStatusMessage;
bc25240 @pbatard [misc] move I/O functions into their own source
authored
149 va_list args;
150 int n;
151
152 va_start(args, format);
b006e6a @pbatard [ui] better bad blocks reports
authored
153 n = safe_vsnprintf(p, sizeof(szStatusMessage)-1, format, args); // room for NUL
bc25240 @pbatard [misc] move I/O functions into their own source
authored
154 va_end(args);
155
b006e6a @pbatard [ui] better bad blocks reports
authored
156 p += (n < 0)?sizeof(szStatusMessage)-1:n;
bc25240 @pbatard [misc] move I/O functions into their own source
authored
157
b006e6a @pbatard [ui] better bad blocks reports
authored
158 while((p>szStatusMessage) && (isspace(p[-1])))
bc25240 @pbatard [misc] move I/O functions into their own source
authored
159 *--p = '\0';
160
161 *p = '\0';
162
d1e4ba2 @pbatard [ntfs] add bootable NTFS support
authored
163 if (debug)
164 uprintf("%s\n", szStatusMessage);
165
b006e6a @pbatard [ui] better bad blocks reports
authored
166 if ((duration) || (!bStatusTimerArmed)) {
f4ed6e4 @pbatard [misc] fix broken DOS, fix invalid labels and UI improvements
authored
167 SendMessageLU(GetDlgItem(hMainDialog, IDC_STATUS), SB_SETTEXTA,
168 SBT_OWNERDRAW | 0, szStatusMessage);
b006e6a @pbatard [ui] better bad blocks reports
authored
169 }
170
171 if (duration) {
17132c3 @pbatard [ui] progress bar improvements
authored
172 SetTimer(hMainDialog, TID_MESSAGE, duration, PrintStatusTimeout);
b006e6a @pbatard [ui] better bad blocks reports
authored
173 bStatusTimerArmed = TRUE;
174 }
bc25240 @pbatard [misc] move I/O functions into their own source
authored
175 }
5065c02 @pbatard [ui] report errors to the user
authored
176
177 const char* StrError(DWORD error_code)
178 {
179 if ( (!IS_ERROR(error_code)) || (SCODE_CODE(error_code) == ERROR_SUCCESS)) {
180 return "Success";
181 }
182 if (SCODE_FACILITY(error_code) != FACILITY_STORAGE) {
183 uprintf("StrError: non storage - %08X (%X)\n", error_code, SCODE_FACILITY(error_code));
184 SetLastError(error_code);
185 return WindowsErrorString();
186 }
187 switch (SCODE_CODE(error_code)) {
188 case ERROR_GEN_FAILURE:
189 return "Undetermined error while formatting";
190 case ERROR_INCOMPATIBLE_FS:
191 return "Cannot use the selected file system for this media";
192 case ERROR_ACCESS_DENIED:
193 return "Access to the media is denied";
194 case ERROR_WRITE_PROTECT:
195 return "Media is write protected";
196 case ERROR_DEVICE_IN_USE:
197 return "The device is in use by another process\n"
198 "Please close any other process that may be accessing the device";
199 case ERROR_CANT_QUICK_FORMAT:
200 return "Quick format is not available for this device";
201 case ERROR_LABEL_TOO_LONG:
202 return "The volume label is invalid";
203 case ERROR_INVALID_CLUSTER_SIZE:
d81525d @pbatard [misc] added README/TODO
authored
204 return "The selected cluster size is not valid for this device";
5065c02 @pbatard [ui] report errors to the user
authored
205 case ERROR_INVALID_VOLUME_SIZE:
206 return "The volume size is invalid";
207 case ERROR_NO_MEDIA_IN_DRIVE:
208 return "Please insert a media in drive";
209 case ERROR_NOT_SUPPORTED:
210 return "An unsupported command was received";
211 case ERROR_NOT_ENOUGH_MEMORY:
212 return "Memory allocation error";
213 case ERROR_READ_FAULT:
214 return "Read error";
215 case ERROR_WRITE_FAULT:
216 return "Write error";
217 case ERROR_OPEN_FAILED:
5722300 @pbatard v1.0.4 (#100)
authored
218 return "Could not open media. It may be in use by another process.\n"
219 "Please re-plug the media and try again";
5065c02 @pbatard [ui] report errors to the user
authored
220 case ERROR_PARTITION_FAILURE:
221 return "Error while partitioning drive";
222 case ERROR_CANNOT_COPY:
472db8b @pbatard [iso] bootable NTFS from ISO image [EXPERIMENTAL]
authored
223 return "Could not copy files to target drive";
5065c02 @pbatard [ui] report errors to the user
authored
224 case ERROR_CANCELLED:
225 return "Cancelled by user";
226 case ERROR_CANT_START_THREAD:
227 return "Unable to create formatting thread";
192c3d6 @pbatard [bb] progress bar and cancellation support
authored
228 case ERROR_BADBLOCKS_FAILURE:
229 return "Bad blocks check didn't complete";
fd0e6d4 @pbatard [iso] UI ISO support improvements
authored
230 case ERROR_ISO_SCAN:
231 return "ISO image scan failure";
232 case ERROR_ISO_EXTRACT:
7753c01 @pbatard v1.1.0 (#131)
authored
233 return "ISO image extraction failure";
472db8b @pbatard [iso] bootable NTFS from ISO image [EXPERIMENTAL]
authored
234 case ERROR_CANT_REMOUNT_VOLUME:
235 return "Unable to remount volume. You may have to use the\n"
236 "mountvol.exe command to make your device accessible again";
db76e39 @pbatard [pe] Improve PE and add Windows 2003 support
authored
237 case ERROR_CANT_PATCH:
238 return "Unable to patch/setup files for boot";
5065c02 @pbatard [ui] report errors to the user
authored
239 default:
5722300 @pbatard v1.0.4 (#100)
authored
240 uprintf("Unknown error: %08X\n", error_code);
5065c02 @pbatard [ui] report errors to the user
authored
241 SetLastError(error_code);
242 return WindowsErrorString();
243 }
244 }
Something went wrong with that request. Please try again.