Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lpc-vector-checksum.c 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /***************************************************************************
  2. * https://github.com/dhylands/projects/blob/master/lpc/lpc-vector-checksum/lpc-vector-checksum.c
  3. *
  4. * Copyright (c) 2012 by Dave Hylands
  5. * All Rights Reserved
  6. *
  7. * Permission is granted to any individual or institution to use, copy,
  8. * modify, or redistribute this file so long as it is not sold for profit,
  9. * and that this copyright notice is retained.
  10. *
  11. ***************************************************************************
  12. *
  13. * This program calculates the vector checksum used in LPC17xx binary
  14. * images.
  15. *
  16. * Usage: lpc-vector-checksum file
  17. *
  18. ***************************************************************************/
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <stdint.h>
  22. #include <errno.h>
  23. #include <string.h>
  24. /***************************************************************************/
  25. /**
  26. * update_vector_checksum
  27. *
  28. * The algorithim is to write the checksum such that the checksum of the
  29. * first 8 words is equal to zero.
  30. *
  31. * The LPC1768 uses little-endian, and this particular routine assumes
  32. * that it's running on a little-endian architecture.
  33. */
  34. static int update_vector_checksum( const char *filename )
  35. {
  36. uint32_t sum;
  37. uint32_t header[8];
  38. FILE *fs;
  39. int i;
  40. if (( fs = fopen( filename, "r+b" )) == NULL )
  41. {
  42. fprintf( stderr, "Unable to open '%s' for reading/writing (%d): %s\n",
  43. filename, errno, strerror( errno ));
  44. return 0;
  45. }
  46. if ( fread( header, sizeof( header ), 1, fs ) != 1 )
  47. {
  48. fprintf( stderr, "Failed to read header from '%s' (perhaps the file is too small?)",
  49. filename );
  50. fclose( fs );
  51. return 0;
  52. }
  53. sum = 0;
  54. for ( i = 0; i < 7; i++ )
  55. {
  56. sum += header[i];
  57. }
  58. printf( "sum = 0x%08x, value to write = 0x%08x\n", sum, -sum );
  59. /* write back the checksum to location 7
  60. * http://sigalrm.blogspot.jp/2011/10/cortex-m3-exception-vector-checksum.html
  61. */
  62. fseek(fs, 0x1c, SEEK_SET);
  63. sum = -sum;
  64. fwrite(&sum, 4, 1, fs);
  65. fclose( fs );
  66. return 1;
  67. }
  68. /***************************************************************************/
  69. /**
  70. * main
  71. */
  72. int main( int argc, char **argv )
  73. {
  74. int arg;
  75. if ( argc < 2)
  76. {
  77. fprintf( stderr, "Usage: lpc-vector-checksum file ...\n" );
  78. exit( 1 );
  79. }
  80. for ( arg = 1; arg < argc; arg++ )
  81. {
  82. update_vector_checksum( argv[ arg ]);
  83. }
  84. exit( 0 );
  85. return 0;
  86. }