main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /**
  2. * u3_tool - U3 USB stick manager
  3. * Copyright (C) 2007 Daviedev, daviedev@users.sourceforge.net
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include "u3.h"
  26. #include "u3_commands.h"
  27. #include "u3_scsi.h"
  28. #include "u3_error.h"
  29. #include "secure_input.h"
  30. #include "display_progress.h"
  31. #define TRUE 1
  32. #define FALSE 0
  33. #define MAX_SIZE_STRING_LENGTH 100
  34. #define MAX_FILENAME_STRING_LENGTH 1024
  35. #define MAX_PASSWORD_LENGTH 1024
  36. char *version = "0.1";
  37. int debug = 0;
  38. int batch_mode = 0;
  39. enum action_t { unknown, load, partition, dump, unlock, change_password,
  40. enable_security, disable_security };
  41. /**
  42. * Ask confirmation of user.
  43. *
  44. * @returns TRUE if user wants to continue else FALSE to abort.
  45. */
  46. int confirm() {
  47. char c;
  48. int retval;
  49. int done;
  50. if (batch_mode != 0)
  51. return TRUE;
  52. retval = FALSE;
  53. done = FALSE;
  54. do {
  55. printf("\nAre you sure you want to continue? [yn] ");
  56. c = fgetc(stdin);
  57. if (c == 'y' || c == 'Y') {
  58. retval = TRUE;
  59. done = TRUE;
  60. } else if (c == 'n' || c == 'N') {
  61. retval = FALSE;
  62. done = TRUE;
  63. }
  64. // drop any extra chars and the new line
  65. while (c != '\n' && c != EOF) {
  66. c = fgetc(stdin);
  67. }
  68. } while (!done);
  69. return retval;
  70. }
  71. /********************************** Actions ***********************************/
  72. int do_load(u3_handle_t *device, char *iso_filename) {
  73. struct stat file_stat;
  74. struct part_info pinfo;
  75. uint32_t cd_size;
  76. FILE *fp;
  77. uint8_t buffer[U3_BLOCK_SIZE];
  78. unsigned int bytes_read=0;
  79. unsigned int block_num=0;
  80. unsigned int block_cnt=0;
  81. // determine file size
  82. if (stat(iso_filename, &file_stat) == -1) {
  83. perror("Failed stating iso file");
  84. return EXIT_FAILURE;
  85. }
  86. if (file_stat.st_size == 0) {
  87. fprintf(stderr, "ISO file is empty\n");
  88. return EXIT_FAILURE;
  89. }
  90. cd_size = file_stat.st_size / U3_SECTOR_SIZE;
  91. if (file_stat.st_size % U3_SECTOR_SIZE)
  92. cd_size++;
  93. block_cnt = file_stat.st_size / U3_BLOCK_SIZE;
  94. if (file_stat.st_size % U3_BLOCK_SIZE)
  95. block_cnt++;
  96. // check partition size
  97. if (u3_partition_info(device, &pinfo) != U3_SUCCESS) {
  98. fprintf(stderr, "u3_partition_info() failed: %s\n",
  99. u3_error_msg(device));
  100. return EXIT_FAILURE;
  101. }
  102. if (cd_size > pinfo.cd_size) {
  103. fprintf(stderr, "CD image(%lu byte) is to big for current cd "
  104. "partition(%u byte), please repartition device.\n",
  105. file_stat.st_size,
  106. pinfo.cd_size * U3_SECTOR_SIZE);
  107. return EXIT_FAILURE;
  108. }
  109. // open file
  110. if ((fp = fopen(iso_filename, "rb")) == NULL) {
  111. perror("Failed opening iso file");
  112. return EXIT_FAILURE;
  113. }
  114. // write file to device
  115. block_num = 0;
  116. do {
  117. display_progress(block_num, block_cnt);
  118. bytes_read = fread(buffer, sizeof(uint8_t), U3_BLOCK_SIZE, fp);
  119. if (bytes_read != U3_BLOCK_SIZE) {
  120. if (ferror(fp)) {
  121. fclose(fp);
  122. perror("\nFailed reading iso file");
  123. return EXIT_FAILURE;
  124. } else if (bytes_read == 0) {
  125. continue;
  126. } else if (bytes_read > U3_BLOCK_SIZE) {
  127. fprintf(stderr, "\nRead more bytes then requested, impossible");
  128. return EXIT_FAILURE;
  129. } else {
  130. // zeroize rest of buffer to prevent writing garbage
  131. memset(buffer+bytes_read, 0, U3_BLOCK_SIZE-bytes_read);
  132. }
  133. }
  134. if (u3_cd_write(device, block_num, buffer) != U3_SUCCESS) {
  135. fclose(fp);
  136. fprintf(stderr, "\nu3_cd_write() failed: %s\n", u3_error_msg(device));
  137. return EXIT_FAILURE;
  138. }
  139. block_num++;
  140. } while (!feof(fp));
  141. display_progress(block_num, block_cnt);
  142. putchar('\n');
  143. fclose(fp);
  144. printf("OK\n");
  145. return EXIT_SUCCESS;
  146. }
  147. int do_partition(u3_handle_t *device, char *size_string) {
  148. uint64_t size;
  149. uint32_t cd_sectors;
  150. // determine file size
  151. size = strtoll(size_string, NULL, 0);
  152. cd_sectors = size / U3_SECTOR_SIZE;
  153. if (size % U3_SECTOR_SIZE)
  154. cd_sectors++;
  155. // repartition
  156. if (u3_partition(device, cd_sectors) != U3_SUCCESS) {
  157. fprintf(stderr, "u3_partition() failed: %s\n", u3_error_msg(device));
  158. return EXIT_FAILURE;
  159. }
  160. // reset device to make partitioning active
  161. if (u3_reset(device) != U3_SUCCESS) {
  162. fprintf(stderr, "u3_reset() failed: %s\n", u3_error_msg(device));
  163. return EXIT_FAILURE;
  164. }
  165. return EXIT_SUCCESS;
  166. }
  167. int do_unlock(u3_handle_t *device, char *password) {
  168. int result=0;
  169. if (u3_unlock(device, password, &result) != U3_SUCCESS) {
  170. fprintf(stderr, "u3_unlock() failed: %s\n", u3_error_msg(device));
  171. return EXIT_FAILURE;
  172. }
  173. if (result) {
  174. printf("OK\n");
  175. return EXIT_SUCCESS;
  176. } else {
  177. printf("Password incorrect\n");
  178. return EXIT_FAILURE;
  179. }
  180. }
  181. int do_change_password(u3_handle_t *device, char *password,
  182. char *new_password)
  183. {
  184. int result=0;
  185. if (u3_change_password(device, password, new_password, &result)
  186. != U3_SUCCESS)
  187. {
  188. fprintf(stderr, "u3_change_password() failed: %s\n", u3_error_msg(device));
  189. return EXIT_FAILURE;
  190. }
  191. if (result) {
  192. printf("OK\n");
  193. return EXIT_SUCCESS;
  194. } else {
  195. printf("Password incorrect\n");
  196. return EXIT_FAILURE;
  197. }
  198. }
  199. int do_enable_security(u3_handle_t *device, char *password) {
  200. if (u3_enable_security(device, password) != U3_SUCCESS) {
  201. fprintf(stderr, "u3_enable_security() failed: %s\n",
  202. u3_error_msg(device));
  203. return EXIT_FAILURE;
  204. }
  205. return EXIT_SUCCESS;
  206. }
  207. int do_disable_security(u3_handle_t *device, char *password) {
  208. int result=0;
  209. if (u3_disable_security(device, password, &result) != U3_SUCCESS) {
  210. fprintf(stderr, "u3_disable_security() failed: %s\n",
  211. u3_error_msg(device));
  212. return EXIT_FAILURE;
  213. }
  214. if (result) {
  215. printf("OK\n");
  216. return EXIT_SUCCESS;
  217. } else {
  218. printf("Password incorrect\n");
  219. return EXIT_FAILURE;
  220. }
  221. }
  222. int do_dump(u3_handle_t *device) {
  223. int retval = EXIT_SUCCESS;
  224. struct part_info pinfo;
  225. struct dpart_info dpinfo;
  226. struct chip_info cinfo;
  227. struct property_03 device_properties;
  228. struct property_0C security_properties;
  229. if (u3_partition_info(device, &pinfo) != U3_SUCCESS) {
  230. fprintf(stderr, "u3_partition_info() failed: %s\n",
  231. u3_error_msg(device));
  232. retval = EXIT_FAILURE;
  233. } else {
  234. printf("Partition info:\n");
  235. printf(" - Partition count: 0x%.2x\n", pinfo.partition_count);
  236. printf(" - Data partition size: %d byte(0x%.8x)\n",
  237. pinfo.data_size * U3_SECTOR_SIZE, pinfo.data_size);
  238. printf(" - Unknown1: 0x%.8x\n", pinfo.unknown1);
  239. printf(" - CD size: %d byte(0x%.8x)\n", pinfo.cd_size*U3_SECTOR_SIZE,
  240. pinfo.cd_size);
  241. printf(" - Unknown2: 0x%.8x\n", pinfo.unknown2);
  242. printf("\n");
  243. }
  244. if (u3_data_partition_info(device, &dpinfo) != U3_SUCCESS) {
  245. fprintf(stderr, "u3_data_partition_info() failed: %s\n",
  246. u3_error_msg(device));
  247. retval = EXIT_FAILURE;
  248. } else {
  249. printf("Data partition info:\n");
  250. printf(" - Data partition size: %d byte(0x%.8x)\n",
  251. dpinfo.total_size * U3_SECTOR_SIZE, dpinfo.total_size);
  252. printf(" - Secured zone size: 0x%.8x\n", dpinfo.secured_size);
  253. printf(" - Unlocked: 0x%.8x\n", dpinfo.unlocked);
  254. printf(" - Password try: 0x%.8x\n", dpinfo.pass_try);
  255. printf("\n");
  256. }
  257. if (u3_chip_info(device, &cinfo) != U3_SUCCESS) {
  258. fprintf(stderr, "u3_chip_info() failed: %s\n",
  259. u3_error_msg(device));
  260. retval = EXIT_FAILURE;
  261. } else {
  262. printf("Chip info:\n");
  263. printf(" - Manufacturer: %.*s\n", U3_MAX_CHIP_MANUFACTURER_LEN,
  264. cinfo.manufacturer);
  265. printf(" - Revision: %.*s\n", U3_MAX_CHIP_REVISION_LEN,
  266. cinfo.revision);
  267. printf("\n");
  268. }
  269. if (u3_read_device_property(device, 0x03,
  270. (uint8_t *) &device_properties,
  271. sizeof(device_properties)
  272. ) != U3_SUCCESS)
  273. {
  274. fprintf(stderr, "u3_read_device_property() failed for "
  275. "property 0x03: %s\n", u3_error_msg(device));
  276. retval = EXIT_FAILURE;
  277. } else {
  278. if (device_properties.hdr.length !=
  279. sizeof(device_properties))
  280. {
  281. fprintf(stderr, "Length of property 0x03 is not the "
  282. "expected length. (len=%u)\n",
  283. device_properties.hdr.length);
  284. retval = EXIT_FAILURE;
  285. } else {
  286. printf("Property page 0x03:\n");
  287. printf(" - Device size: 0x%.8x\n",
  288. device_properties.device_size);
  289. printf(" - Device serial: %.*s\n",U3_MAX_SERIAL_LEN,
  290. device_properties.serial);
  291. printf(" - Full record length: 0x%.8x\n",
  292. device_properties.full_length);
  293. printf(" - Unknown1: 0x%.2x\n",
  294. device_properties.unknown1);
  295. printf(" - Unknown2: 0x%.8x\n",
  296. device_properties.unknown2);
  297. printf(" - Unknown3: 0x%.8x\n",
  298. device_properties.unknown3);
  299. printf("\n");
  300. }
  301. }
  302. if (u3_read_device_property(device, 0x0C,
  303. (uint8_t *) &security_properties,
  304. sizeof(security_properties)
  305. ) != U3_SUCCESS)
  306. {
  307. fprintf(stderr, "u3_read_device_property() failed for "
  308. "property 0x0C: %s\n", u3_error_msg(device));
  309. retval = EXIT_FAILURE;
  310. } else {
  311. if (security_properties.hdr.length !=
  312. sizeof(security_properties))
  313. {
  314. fprintf(stderr, "Length of property 0x0C is not the "
  315. "expected length. (len=%u)\n",
  316. security_properties.hdr.length);
  317. retval = EXIT_FAILURE;
  318. } else {
  319. printf("Property page 0x0C:\n");
  320. printf(" - Max. pass. try: %u",
  321. security_properties.max_pass_try);
  322. printf("\n");
  323. }
  324. }
  325. return EXIT_SUCCESS;
  326. }
  327. /************************************ Main ************************************/
  328. void usage(const char *name) {
  329. printf("u3_tool %s\n", version);
  330. printf("U3 USB stick manager\n");
  331. printf("\n");
  332. printf("Usage: %s [options] <device name>\n", name);
  333. printf("\n");
  334. printf("Options:\n");
  335. printf("\t-c Change password\n");
  336. printf("\t-d Disable device security\n");
  337. printf("\t-D Dump all raw info(for debug)\n");
  338. printf("\t-e Enable device security\n");
  339. printf("\t-h Print this help message\n");
  340. //TODO: printf("\t-i Display device info\n");
  341. printf("\t-l <cd image> Load CD image into device\n");
  342. printf("\t-p <cd size> Repartition device\n");
  343. printf("\t-u Unlock device\n");
  344. printf("\t-v Use verbose output\n");
  345. printf("\t-V Print version information\n");
  346. printf("\n");
  347. printf("The device name depends on the used subsystem.\n");
  348. printf("Examples: '/dev/sda0'(sg), 'scan'(USB),'vid:pid'(USB), "
  349. "'e'(Windows)\n");
  350. }
  351. void print_version() {
  352. printf("u3_tool %s\n", version);
  353. printf("\n");
  354. printf("Copyright (C) 2007\n");
  355. printf("This is free software; see the source for copying "
  356. "conditions. There is NO\n warranty; not even for "
  357. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
  358. }
  359. int main(int argc, char *argv[]) {
  360. u3_handle_t device;
  361. int c;
  362. enum action_t action = unknown;
  363. char *device_name;
  364. char filename_string[MAX_FILENAME_STRING_LENGTH+1];
  365. char size_string[MAX_SIZE_STRING_LENGTH+1];
  366. int ask_password = TRUE;
  367. char password[MAX_PASSWORD_LENGTH+1];
  368. int ask_new_password = TRUE;
  369. char new_password[MAX_PASSWORD_LENGTH+1];
  370. int retval = EXIT_SUCCESS;
  371. //
  372. // parse options
  373. //
  374. while ((c = getopt(argc, argv, "cdDehl:p:uvVz")) != -1) {
  375. switch (c) {
  376. case 'c':
  377. action = change_password;
  378. break;
  379. case 'd':
  380. action = disable_security;
  381. break;
  382. case 'e':
  383. action = enable_security;
  384. break;
  385. case 'l':
  386. action = load;
  387. strncpy(filename_string, optarg, MAX_FILENAME_STRING_LENGTH);
  388. filename_string[MAX_FILENAME_STRING_LENGTH] = '\0';
  389. break;
  390. case 'p':
  391. action = partition;
  392. strncpy(size_string, optarg, MAX_SIZE_STRING_LENGTH);
  393. size_string[MAX_SIZE_STRING_LENGTH] = '\0';
  394. break;
  395. case 'u':
  396. action = unlock;
  397. break;
  398. case 'v':
  399. debug = 1;
  400. break;
  401. case 'V':
  402. print_version();
  403. exit(EXIT_SUCCESS);
  404. break;
  405. case 'D':
  406. action = dump;
  407. break;
  408. case 'h':
  409. default:
  410. usage(argv[0]);
  411. exit(EXIT_FAILURE);
  412. break;
  413. }
  414. }
  415. //
  416. // parse arguments
  417. //
  418. if (argc-optind < 1) {
  419. fprintf(stderr, "Not enough arguments\n");
  420. usage(argv[0]);
  421. exit(EXIT_FAILURE);
  422. }
  423. device_name = argv[optind];
  424. if ((action == unlock || action == change_password
  425. || action == disable_security)
  426. && ask_password)
  427. {
  428. int proceed = 0;
  429. do {
  430. fprintf(stderr, "Enter password: ");
  431. secure_input(password, sizeof(password));
  432. if (strlen(password) == 0) {
  433. fprintf(stderr, "Password Empty\n");
  434. } else {
  435. proceed = 1;
  436. }
  437. } while (!proceed);
  438. }
  439. if ((action == change_password || action == enable_security)
  440. && ask_new_password)
  441. {
  442. int proceed = 0;
  443. char validate_password[MAX_PASSWORD_LENGTH+1];
  444. do {
  445. fprintf(stderr, "Enter new password: ");
  446. secure_input(new_password, sizeof(new_password));
  447. if (strlen(new_password) == 0) {
  448. fprintf(stderr, "Password Empty\n");
  449. continue;
  450. }
  451. fprintf(stderr, "Reenter new password: ");
  452. secure_input(validate_password, sizeof(validate_password));
  453. if (strcmp(new_password, validate_password) == 0) {
  454. proceed = 1;
  455. } else {
  456. fprintf(stderr, "Passwords don't match\n");
  457. }
  458. } while (!proceed);
  459. }
  460. //
  461. // open the device
  462. //
  463. if (u3_open(&device, device_name)) {
  464. fprintf(stderr, "Error opening device: %s\n", u3_error_msg(&device));
  465. exit(EXIT_FAILURE);
  466. }
  467. //
  468. // preform action
  469. //
  470. switch (action) {
  471. case load:
  472. retval = do_load(&device, filename_string);
  473. break;
  474. case partition:
  475. printf("\n");
  476. printf("WARNING: Loading a new cd image causes the ");
  477. printf("whole device to be whiped. This INCLUDES\n ");
  478. printf("the data partition.\n");
  479. printf("I repeat: ANY EXCISTING DATA WILL BE LOST!\n");
  480. if (confirm())
  481. retval = do_partition(&device, size_string);
  482. break;
  483. case dump:
  484. retval = do_dump(&device);
  485. break;
  486. case unlock:
  487. retval = do_unlock(&device, password);
  488. break;
  489. case change_password:
  490. retval = do_change_password(&device, password, new_password);
  491. break;
  492. case enable_security:
  493. retval = do_enable_security(&device, new_password);
  494. break;
  495. case disable_security:
  496. retval = do_disable_security(&device, password);
  497. break;
  498. default:
  499. printf("Don't know what to do... please specify an action\n");
  500. break;
  501. }
  502. //
  503. // clean up
  504. //
  505. u3_close(&device);
  506. return retval;
  507. }