ubicrc32.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  12. * the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /*
  19. * Calculate CRC32 with UBI start value (0xFFFFFFFF) for a given binary image.
  20. *
  21. * Author: Oliver Lohmann
  22. */
  23. #define PROGRAM_NAME "ubicrc32"
  24. #include <stdio.h>
  25. #include <stdint.h>
  26. #include <stdlib.h>
  27. #include <getopt.h>
  28. #include <unistd.h>
  29. #include <mtd/ubi-media.h>
  30. #include <crc32.h>
  31. #include "common.h"
  32. #define BUFSIZE 4096
  33. static const char doc[] = PROGRAM_NAME " version " VERSION
  34. " - a tool to calculate CRC32 with UBI start value (0xFFFFFFFF)";
  35. static const char optionsstr[] =
  36. "-h, --help print help message\n"
  37. "-V, --version print program version";
  38. static const char usage[] =
  39. "Usage: " PROGRAM_NAME " <file to calculate CRC32 for> [-h] [--help]";
  40. static const struct option long_options[] = {
  41. { .name = "help", .has_arg = 0, .flag = NULL, .val = 'h' },
  42. { .name = "version", .has_arg = 0, .flag = NULL, .val = 'V' },
  43. { NULL, 0, NULL, 0},
  44. };
  45. static int parse_opt(int argc, char * const argv[])
  46. {
  47. while (1) {
  48. int key;
  49. key = getopt_long(argc, argv, "hV", long_options, NULL);
  50. if (key == -1)
  51. break;
  52. switch (key) {
  53. case 'h':
  54. printf("%s\n\n", doc);
  55. printf("%s\n\n", usage);
  56. printf("%s\n", optionsstr);
  57. exit(EXIT_SUCCESS);
  58. case 'V':
  59. common_print_version();
  60. exit(EXIT_SUCCESS);
  61. case ':':
  62. return errmsg("parameter is missing");
  63. default:
  64. fprintf(stderr, "Use -h for help\n");
  65. return -1;
  66. }
  67. }
  68. return 0;
  69. }
  70. int main(int argc, char * const argv[])
  71. {
  72. int err = 0;
  73. uint32_t crc = UBI_CRC32_INIT;
  74. char buf[BUFSIZE];
  75. FILE *fp = stdin;
  76. err = parse_opt(argc, argv);
  77. if (err)
  78. return err;
  79. if (optind < argc) {
  80. fp = fopen(argv[optind], "r");
  81. if (!fp)
  82. return sys_errmsg("cannot open \"%s\"", argv[1]);
  83. }
  84. while (!feof(fp)) {
  85. size_t read;
  86. read = fread(buf, 1, BUFSIZE, fp);
  87. if (ferror(fp)) {
  88. sys_errmsg("cannot read input file");
  89. err = -1;
  90. goto out_close;
  91. }
  92. crc = mtd_crc32(crc, buf, read);
  93. }
  94. printf("0x%08x\n", crc);
  95. out_close:
  96. if (fp != stdin)
  97. fclose(fp);
  98. return err;
  99. }