Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- static void print_usage()
- {
- printf("Usage:\n");
- printf(" setmac [file] [MAC address]\n");
- printf("MAC addresses should be one of two forms:\n");
- printf(" xx:yy:zz:ss:tt:ww\n");
- printf(" xxyyzzssttww\n");
- }
- static int is_hex_value(char value)
- {
- if (value >= '0' && value <= '9') return 1;
- if (value >= 'a' && value <= 'f') return 1;
- if (value >= 'A' && value <= 'F') return 1;
- return 0;
- }
- static unsigned char get_hex_value(char value)
- {
- if (value >= '0' && value <= '9') return value - '0';
- if (value >= 'a' && value <= 'f') return (value - 'a') + 10;
- if (value >= 'A' && value <= 'F') return (value - 'A') + 10;
- return 0xff;
- }
- static unsigned char get_eeprom_checksum(unsigned short *eeprom)
- {
- int checksum = (int)0xffffffa5;
- checksum ^= eeprom[0] & 0x00ff;
- checksum <<= 24;
- checksum >>= 24;
- checksum = (checksum << 1) | ((checksum < 0) ? 1 : 0);
- checksum <<= 24;
- checksum >>= 24;
- for (int i = 1; i < 0x80; i++)
- {
- unsigned char upper = eeprom[i] >> 8;
- unsigned char lower = eeprom[i] & 0x00ff;
- unsigned char xorval = upper ^ lower;
- checksum ^= xorval;
- checksum <<= 24;
- checksum >>= 24;
- checksum = (checksum << 1) | ((checksum < 0) ? 1 : 0);
- checksum <<= 24;
- checksum >>= 24;
- }
- return (unsigned char)checksum;
- }
- int main(int argc, char **argv)
- {
- if (argc < 3)
- {
- print_usage();
- return 1;
- }
- FILE *input = fopen(argv[1], "rb");
- if (!input)
- {
- fprintf(stderr, "Unable to open file %s for reading.\n", argv[1]);
- return 1;
- }
- fseek(input, 0, SEEK_END);
- long file_size = ftell(input);
- fseek(input, 0, SEEK_SET);
- if (file_size != 0x100)
- {
- fprintf(stderr, "File %s does not appear to be a 93C56 EEPROM dump.\n", argv[1]);
- fclose(input);
- return 1;
- }
- int length = strlen(argv[2]);
- if (length != 12 && length != 17)
- {
- fprintf(stderr, "'%s' does not appear to be a MAC address.\n", argv[2]);
- print_usage();
- fclose(input);
- return 1;
- }
- unsigned char mac[6];
- int mac_pos = 0;
- int upper_nybble = 1;
- for (int i = 0; i < length; i++)
- {
- if (argv[2][i] == ':') continue;
- if (!is_hex_value(argv[2][i]))
- {
- fprintf(stderr, "'%c' is not a hexadecimal value.\n", argv[2][i]);
- print_usage();
- fclose(input);
- return 1;
- }
- if (upper_nybble)
- {
- mac[mac_pos] = get_hex_value(argv[2][i]) << 4;
- }
- else
- {
- mac[mac_pos] |= get_hex_value(argv[2][i]);
- mac_pos++;
- }
- upper_nybble = !upper_nybble;
- }
- unsigned short eeprom[0x80];
- for (int i = 0; i < 0x80; i++)
- {
- unsigned char upper = 0;
- unsigned char lower = 0;
- fread(&lower, 1, 1, input);
- fread(&upper, 1, 1, input);
- eeprom[i] = (upper << 8) | lower;
- }
- fclose(input);
- eeprom[0x7d] = (mac[0] << 8) | mac[1];
- eeprom[0x7e] = (mac[2] << 8) | mac[3];
- eeprom[0x7f] = (mac[4] << 8) | mac[5];
- unsigned char checksum = get_eeprom_checksum(eeprom);
- eeprom[0x00] &= 0x00ff;
- eeprom[0x00] |= checksum << 8;
- input = fopen(argv[1], "wb");
- if (!input)
- {
- fprintf(stderr, "Unable to open file %s for writing. Check that it is not write-protected.\n", argv[1]);
- return 1;
- }
- for (int i = 0; i < 0x80; i++)
- {
- unsigned char upper = eeprom[i] >> 8;
- unsigned char lower = (unsigned char)eeprom[i];
- fwrite(&lower, 1, 1, input);
- fwrite(&upper, 1, 1, input);
- }
- fclose(input);
- printf("MAC set successfully.\n");
- return 0;
- }
RAW Paste Data