diff --git a/eepromutils/Makefile b/eepromutils/Makefile index 8363b41..e3784d4 100644 --- a/eepromutils/Makefile +++ b/eepromutils/Makefile @@ -24,5 +24,6 @@ uninstall: rm -f $(DESTDIR)$(prefix)/bin/eepmake rm -f $(DESTDIR)$(prefix)/bin/eepdump rm -f $(DESTDIR)$(exec_prefix)/sbin/eepflash.sh + clean: rm -f eepmake eepdump diff --git a/eepromutils/README.md b/eepromutils/README.md index 6c1ce35..78c3a61 100644 --- a/eepromutils/README.md +++ b/eepromutils/README.md @@ -34,16 +34,15 @@ https://www.raspberrypi.org/forums/viewtopic.php?t=108134 ```bash i2cdetect -y 9 0x50 0x50 0 1 2 3 4 5 6 7 8 9 a b c d e f - 00: + 00: 10: 20: 30: 40: 50: 50 - 60: + 60: 70: ``` Normally, you can skip this step, and assume things are working. 3. Flash eep file `sudo ./eepflash.sh -w -t=24c32 -f=eeprom.eep` 4. Enable EEPROM write protection, by undoing step 1 (putting back jumper, or resetting GPIO) - diff --git a/eepromutils/eepdump.c b/eepromutils/eepdump.c index 5fca33b..15b5c84 100644 --- a/eepromutils/eepdump.c +++ b/eepromutils/eepdump.c @@ -11,22 +11,24 @@ struct header_t header; struct atom_t atom; struct vendor_info_d vinf; struct gpio_map_d gpiomap; -unsigned char* data; +unsigned char *data; -int read_bin(char *in, char *outf) { +int read_bin(char *in, char *outf) +{ uint16_t crc; FILE *fp, *out; int i, size_in; + long pos; uint32_t j; - fp=fopen(in, "r"); + fp = fopen(in, "r"); if (!fp) { printf("Error reading file %s\n", in); return -1; } - i = fseek(fp,0,SEEK_END); + i = fseek(fp, 0, SEEK_END); if (i) { printf("error %i\n", i); fclose(fp); @@ -41,61 +43,68 @@ int read_bin(char *in, char *outf) { return -1; } - out=fopen(outf, "w"); + out = fopen(outf, "w"); if (!out) { printf("Error writing file %s\n", outf); return -1; } - + if (!fread(&header, sizeof(header), 1, fp)) { printf("error reading header\n"); goto err; } - + fprintf(out, "# ---------- Dump generated by eepdump handling format version 0x%02x ----------\n#\n", FORMAT_VERSION); + + if (FORMAT_VERSION != header.ver) + fprintf(out, "# WARNING: format version mismatch!!!\n"); + if (HEADER_SIGN != header.signature) + fprintf(out, "# WARNING: format signature mismatch!!!\n"); + if ((uint32_t)size_in < header.eeplen) + fprintf(out, "# WARNING: Incomplete file\n"); - if (FORMAT_VERSION!=header.ver) fprintf(out, "# WARNING: format version mismatch!!!\n"); - if (HEADER_SIGN!=header.signature) fprintf(out, "# WARNING: format signature mismatch!!!\n"); - if ((uint32_t)size_in < header.eeplen) fprintf(out, "# WARNING: Incomplete file\n"); - - fprintf(out, "# --Header--\n# signature=0x%08x\n# version=0x%02x\n# reserved=%u\n# numatoms=%u\n# eeplen=%u\n# ----------\n\n\n", header.signature, header.ver, header.res, header.numatoms, header.eeplen); + fprintf(out, "# --Header--\n# signature=0x%08x\n# version=0x%02x\n# reserved=%u\n# numatoms=%u\n# eeplen=%u\n# ----------\n\n\n", + header.signature, header.ver, header.res, header.numatoms, header.eeplen); - if (FORMAT_VERSION!=header.ver && HEADER_SIGN!=header.signature) { + if (FORMAT_VERSION != header.ver && HEADER_SIGN != header.signature) { printf("header version and signature mismatch, maybe wrong file?\n"); goto err; } size_in -= sizeof(header); - for (i = 0; i>16, vinf.serial[2] & 0xffff, - vinf.serial[1]>>16, vinf.serial[1] & 0xffff, - vinf.serial[0]); + vinf.serial[3], + vinf.serial[2] >> 16, vinf.serial[2] & 0xffff, + vinf.serial[1] >> 16, vinf.serial[1] & 0xffff, + vinf.serial[0]); fprintf(out, "product_id 0x%04x\n", vinf.pid); fprintf(out, "product_ver 0x%04x\n", vinf.pver); - - vinf.vstr = (char *) malloc(vinf.vslen+1); - vinf.pstr = (char *) malloc(vinf.pslen+1); - + + vinf.vstr = (char *)malloc(vinf.vslen + 1); + vinf.pstr = (char *)malloc(vinf.pslen + 1); + if (!fread(vinf.vstr, vinf.vslen, 1, fp)) goto err; if (!fread(vinf.pstr, vinf.pslen, 1, fp)) goto err; - //close strings + /* close strings */ vinf.vstr[vinf.vslen] = 0; vinf.pstr[vinf.pslen] = 0; - + fprintf(out, "vendor \"%s\" # length=%u\n", vinf.vstr, vinf.vslen); fprintf(out, "product \"%s\" # length=%u\n", vinf.pstr, vinf.pslen); - + if (!fread(&crc, CRC_SIZE, 1, fp)) goto err; - } else if (atom.type==ATOM_GPIO_TYPE) { - //decode GPIO map + } else if (atom.type == ATOM_GPIO_TYPE) { + /* decode GPIO map */ if (!fread(&gpiomap, GPIO_SIZE, 1, fp)) goto err; - + fprintf(out, "# GPIO map info\n"); - fprintf(out, "gpio_drive %d\n", gpiomap.flags & 15); //1111 - fprintf(out, "gpio_slew %d\n", (gpiomap.flags & 48)>>4); //110000 - fprintf(out, "gpio_hysteresis %d\n", (gpiomap.flags & 192)>>6); //11000000 + fprintf(out, "gpio_drive %d\n", gpiomap.flags & 15); // 1111 + fprintf(out, "gpio_slew %d\n", (gpiomap.flags & 48) >> 4); // 110000 + fprintf(out, "gpio_hysteresis %d\n", (gpiomap.flags & 192) >> 6); // 11000000 fprintf(out, "back_power %d\n", gpiomap.power); fprintf(out, "# GPIO FUNCTION PULL\n# ---- -------- ----\n"); - for (j = 0; j<28; j++) { - if (gpiomap.pins[j] & (1<<7)) { - //board uses this pin - - char *pull_str = "INVALID"; - switch ((gpiomap.pins[j] & 96)>>5) { //1100000 - case 0: pull_str = "DEFAULT"; - break; - case 1: pull_str = "UP"; - break; - case 2: pull_str = "DOWN"; - break; - case 3: pull_str = "NONE"; - break; + for (j = 0; j < 28; j++) { + if (gpiomap.pins[j] & (1 << 7)) { + /* board uses this pin */ + char *pull_str, *func_str; + + switch ((gpiomap.pins[j] & 96) >> 5) { // 1100000 + case 0: pull_str = "DEFAULT"; break; + case 1: pull_str = "UP"; break; + case 2: pull_str = "DOWN"; break; + case 3: pull_str = "NONE"; break; + default: pull_str = "INVALID"; } - - char *func_str = "INVALID"; - switch ((gpiomap.pins[j] & 7)) { //111 - case 0: func_str = "INPUT"; - break; - case 1: func_str = "OUTPUT"; - break; - case 4: func_str = "ALT0"; - break; - case 5: func_str = "ALT1"; - break; - case 6: func_str = "ALT2"; - break; - case 7: func_str = "ALT3"; - break; - case 3: func_str = "ALT4"; - break; - case 2: func_str = "ALT5"; - break; + + switch ((gpiomap.pins[j] & 7)) { // 111 + case 0: func_str = "INPUT"; break; + case 1: func_str = "OUTPUT"; break; + case 4: func_str = "ALT0"; break; + case 5: func_str = "ALT1"; break; + case 6: func_str = "ALT2"; break; + case 7: func_str = "ALT3"; break; + case 3: func_str = "ALT4"; break; + case 2: func_str = "ALT5"; break; + default: func_str = "INVALID"; } - + fprintf(out, "setgpio %d %s %s\n", j, func_str, pull_str); } } if (!fread(&crc, CRC_SIZE, 1, fp)) goto err; - - } else if (atom.type==ATOM_DT_TYPE) { - //decode DT blob - + + } else if (atom.type == ATOM_DT_TYPE) { + /* decode DT blob */ + fprintf(out, "dt_blob"); - data = (unsigned char *) malloc(atom.dlen-CRC_SIZE); + data = (unsigned char *)malloc(atom.dlen - CRC_SIZE); if (!fread(data, atom.dlen-CRC_SIZE, 1, fp)) goto err; - - for (j = 0; j @@ -13,153 +13,160 @@ #include "eeptypes.h" -//todo: larger initial mallocs +// todo: larger initial mallocs struct header_t header; struct atom_t *custom_atom, vinf_atom, gpio_atom, dt_atom; -struct vendor_info_d* vinf; -struct gpio_map_d* gpiomap; +struct vendor_info_d *vinf; +struct gpio_map_d *gpiomap; + +bool product_serial_set, product_id_set, product_ver_set, vendor_set, product_set, + gpio_drive_set, gpio_slew_set, gpio_hysteresis_set, gpio_power_set; -bool product_serial_set, product_id_set, product_ver_set, vendor_set, product_set, - gpio_drive_set, gpio_slew_set, gpio_hysteresis_set, gpio_power_set; - bool data_receive, has_dt, receive_dt; - + char **data; -char *current_atom; //rearranged to write out +char *current_atom; // rearranged to write out unsigned int data_len, custom_ct, total_size, data_cap, custom_cap; -int write_binary(char* out) { +int write_binary(char *out) +{ FILE *fp; int offset; unsigned int i; short crc; - + fp=fopen(out, "wb"); if (!fp) { printf("Error writing file %s\n", out); return -1; } - + fwrite(&header, sizeof(header), 1, fp); - - - current_atom = (char *) malloc(vinf_atom.dlen+ATOM_SIZE-CRC_SIZE); + + current_atom = (char *)malloc(vinf_atom.dlen + ATOM_SIZE - CRC_SIZE); offset = 0; - //vendor information atom first part - memcpy(current_atom, &vinf_atom, ATOM_SIZE-CRC_SIZE); - offset += ATOM_SIZE-2; - //data first part - memcpy(current_atom+offset, vinf_atom.data, VENDOR_SIZE); - offset += VENDOR_SIZE; - //data strings - memcpy(current_atom+offset, vinf->vstr, vinf->vslen); + + /* vendor information atom first part */ + memcpy(current_atom, &vinf_atom, ATOM_SIZE - CRC_SIZE); + offset += ATOM_SIZE - 2; + + /* data first part */ + memcpy(current_atom + offset, vinf_atom.data, VENDOR_SIZE); + offset += VENDOR_SIZE; + + /* data strings */ + memcpy(current_atom + offset, vinf->vstr, vinf->vslen); offset += vinf->vslen; - memcpy(current_atom+offset, vinf->pstr, vinf->pslen); + memcpy(current_atom + offset, vinf->pstr, vinf->pslen); offset += vinf->pslen; - //vinf last part + + /* vinf last part */ crc = getcrc(current_atom, offset); memcpy(current_atom+offset, &crc, CRC_SIZE); offset += CRC_SIZE; - + fwrite(current_atom, offset, 1, fp); free(current_atom); - - current_atom = (char *) malloc(gpio_atom.dlen+ATOM_SIZE-CRC_SIZE); + + current_atom = (char *)malloc(gpio_atom.dlen + ATOM_SIZE - CRC_SIZE); offset = 0; - //GPIO map first part - memcpy(current_atom, &gpio_atom, ATOM_SIZE-CRC_SIZE); - offset += ATOM_SIZE-CRC_SIZE; - //GPIO data + + /* GPIO map first part */ + memcpy(current_atom, &gpio_atom, ATOM_SIZE - CRC_SIZE); + offset += ATOM_SIZE - CRC_SIZE; + + /* GPIO data */ memcpy(current_atom+offset, gpiomap, GPIO_SIZE); offset += GPIO_SIZE; - //GPIO map last part + + /* GPIO map last part */ crc = getcrc(current_atom, offset); memcpy(current_atom+offset, &crc, CRC_SIZE); offset += CRC_SIZE; - + fwrite(current_atom, offset, 1, fp); free(current_atom); - + if (has_dt) { printf("Writing out DT...\n"); - current_atom = (char *) malloc(dt_atom.dlen+ATOM_SIZE-CRC_SIZE); + current_atom = (char *)malloc(dt_atom.dlen + ATOM_SIZE-CRC_SIZE); offset = 0; - - memcpy(current_atom, &dt_atom, ATOM_SIZE-CRC_SIZE); - offset += ATOM_SIZE-CRC_SIZE; - - memcpy(current_atom+offset, dt_atom.data, dt_atom.dlen-CRC_SIZE); - offset += dt_atom.dlen-CRC_SIZE; - + + memcpy(current_atom, &dt_atom, ATOM_SIZE - CRC_SIZE); + offset += ATOM_SIZE - CRC_SIZE; + + memcpy(current_atom + offset, dt_atom.data, dt_atom.dlen - CRC_SIZE); + offset += dt_atom.dlen - CRC_SIZE; + crc = getcrc(current_atom, offset); - memcpy(current_atom+offset, &crc, CRC_SIZE); + memcpy(current_atom + offset, &crc, CRC_SIZE); offset += CRC_SIZE; - + fwrite(current_atom, offset, 1, fp); free(current_atom); } - for (i = 0; iserial[3], &high1, &vinf->serial[2], &high2, &vinf->serial[1], &vinf->serial[0]); - - vinf->serial[2] |= high1<<16; - vinf->serial[1] |= high2<<16; - - if ((vinf->serial[3]==0) && (vinf->serial[2]==0) && - (vinf->serial[1]==0) && (vinf->serial[0]==0)) { - //read 128 random bits from /dev/urandom - int random_file = open("/dev/urandom", O_RDONLY); - ssize_t result = read(random_file, vinf->serial, 16); + + vinf->serial[2] |= high1 << 16; + vinf->serial[1] |= high2 << 16; + + if ((vinf->serial[3] == 0) && (vinf->serial[2] == 0) && + (vinf->serial[1] == 0) && (vinf->serial[0] == 0)) { + int random_file; + ssize_t result; + + /* read 128 random bits from /dev/urandom */ + random_file = open("/dev/urandom", O_RDONLY); + result = read(random_file, vinf->serial, 16); close(random_file); + if (result <= 0) { printf("Unable to read from /dev/urandom to set up UUID"); return -1; - } - else { - //put in the version + } else { + /* put in the version */ vinf->serial[2] = (vinf->serial[2] & 0xffff0fff) | 0x00004000; - - //put in the variant + + /* put in the variant */ vinf->serial[1] = (vinf->serial[1] & 0x3fffffff) | 0x80000000; - + printf("UUID=%08x-%04x-%04x-%04x-%04x%08x\n", vinf->serial[3], - vinf->serial[2]>>16, vinf->serial[2] & 0xffff, - vinf->serial[1]>>16, vinf->serial[1] & 0xffff, + vinf->serial[2] >> 16, vinf->serial[2] & 0xffff, + vinf->serial[1] >> 16, vinf->serial[1] & 0xffff, vinf->serial[0]); } - } - - } else if (strcmp(cmd, "product_id")==0) { - product_id_set = true; //required field + } else if (strcmp(cmd, "product_id") == 0) { + product_id_set = true; // required field sscanf(c, "%100s %hx", cmd, &vinf->pid); - - } else if (strcmp(cmd, "product_ver")==0) { - product_ver_set = true; //required field + + } else if (strcmp(cmd, "product_ver") == 0) { + product_ver_set = true; // required field sscanf(c, "%100s %hx", cmd, &vinf->pver); - - } else if (strcmp(cmd, "vendor")==0) { - vendor_set = true; //required field - - vinf->vstr = (char*) malloc (256); + + } else if (strcmp(cmd, "vendor") == 0) { + vendor_set = true; // required field + + vinf->vstr = (char *)malloc(256); sscanf(c, "%100s \"%255[^\"]\"", cmd, vinf->vstr); - - total_size-=vinf->vslen; - vinf_atom.dlen-=vinf->vslen; - + + total_size -= vinf->vslen; + vinf_atom.dlen -= vinf->vslen; + vinf->vslen = strlen(vinf->vstr); - - total_size+=vinf->vslen; - vinf_atom.dlen+=vinf->vslen; - - } else if (strcmp(cmd, "product")==0) { + + total_size += vinf->vslen; + vinf_atom.dlen += vinf->vslen; + + } else if (strcmp(cmd, "product") == 0) { product_set = true; //required field - - vinf->pstr = (char*) malloc (256); + + vinf->pstr = (char *)malloc(256); sscanf(c, "%100s \"%255[^\"]\"", cmd, vinf->pstr); - - total_size-=vinf->pslen; - vinf_atom.dlen-=vinf->pslen; - + + total_size -= vinf->pslen; + vinf_atom.dlen -= vinf->pslen; + vinf->pslen = strlen(vinf->pstr); - - total_size+=vinf->pslen; - vinf_atom.dlen+=vinf->pslen; - } - + + total_size += vinf->pslen; + vinf_atom.dlen += vinf->pslen; + /* GPIO map related part */ - else if (strcmp(cmd, "gpio_drive")==0) { - gpio_drive_set = true; //required field - + } else if (strcmp(cmd, "gpio_drive") == 0) { + gpio_drive_set = true; // required field + sscanf(c, "%100s %1x", cmd, &val); - if (val>8 || val<0) printf("Warning: gpio_drive property in invalid region, using default value instead\n"); - else gpiomap->flags |= val; - - - } else if (strcmp(cmd, "gpio_slew")==0) { - gpio_slew_set = true; //required field - + if (val > 8 || val < 0) + printf("Warning: gpio_drive property in invalid region, using default value instead\n"); + else + gpiomap->flags |= val; + + } else if (strcmp(cmd, "gpio_slew") == 0) { + gpio_slew_set = true; // required field + sscanf(c, "%100s %1x", cmd, &val); - - if (val>2 || val<0) printf("Warning: gpio_slew property in invalid region, using default value instead\n"); - else gpiomap->flags |= val<<4; - - } else if (strcmp(cmd, "gpio_hysteresis")==0) { - gpio_hysteresis_set = true; //required field - + + if (val > 2 || val < 0) + printf("Warning: gpio_slew property in invalid region, using default value instead\n"); + else + gpiomap->flags |= val << 4; + + } else if (strcmp(cmd, "gpio_hysteresis") == 0) { + gpio_hysteresis_set = true; // required field + sscanf(c, "%100s %1x", cmd, &val); - - if (val>2 || val<0) printf("Warning: gpio_hysteresis property in invalid region, using default value instead\n"); - else gpiomap->flags |= val<<6; - - } else if (strcmp(cmd, "back_power")==0) { - gpio_power_set = true; //required field - + + if (val > 2 || val < 0) + printf("Warning: gpio_hysteresis property in invalid region, using default value instead\n"); + else + gpiomap->flags |= val << 6; + + } else if (strcmp(cmd, "back_power") == 0) { + gpio_power_set = true; // required field + sscanf(c, "%100s %1x", cmd, &val); - - if (val>2 || val<0) printf("Warning: back_power property in invalid region, using default value instead\n"); - else gpiomap->power = val; - - } else if (strcmp(cmd, "setgpio")==0) { - fn = (char*) malloc (101); - pull = (char*) malloc (101); - + + if (val > 2 || val < 0) + printf("Warning: back_power property in invalid region, using default value instead\n"); + else + gpiomap->power = val; + + } else if (strcmp(cmd, "setgpio") == 0) { + fn = (char *)malloc(101); + pull = (char*)malloc(101); + sscanf(c, "%100s %d %100s %100s", cmd, &val, fn, pull); - - if (val=GPIO_COUNT) printf("Error: GPIO number out of bounds\n"); + + if (val < GPIO_MIN || val >= GPIO_COUNT) + printf("Error: GPIO number out of bounds\n"); else { valid = true; pin = 0; - - if (strcmp(fn, "INPUT")==0) { - //no action - } else if (strcmp(fn, "OUTPUT")==0) { + + if (strcmp(fn, "INPUT") == 0) { + // no action + } else if (strcmp(fn, "OUTPUT") == 0) { pin |= 1; - } else if (strcmp(fn, "ALT0")==0) { + } else if (strcmp(fn, "ALT0") == 0) { pin |= 4; - } else if (strcmp(fn, "ALT1")==0) { + } else if (strcmp(fn, "ALT1") == 0) { pin |= 5; - } else if (strcmp(fn, "ALT2")==0) { + } else if (strcmp(fn, "ALT2") == 0) { pin |= 6; - } else if (strcmp(fn, "ALT3")==0) { + } else if (strcmp(fn, "ALT3") == 0) { pin |= 7; - } else if (strcmp(fn, "ALT4")==0) { + } else if (strcmp(fn, "ALT4") == 0) { pin |= 3; - } else if (strcmp(fn, "ALT5")==0) { + } else if (strcmp(fn, "ALT5") == 0) { pin |= 2; } else { printf("Error at setgpio: function type not recognised\n"); valid=false; } - - if (strcmp(pull, "DEFAULT")==0) { - //no action - } else if (strcmp(pull, "UP")==0) { - pin |= 1<<5; - } else if (strcmp(pull, "DOWN")==0) { - pin |= 2<<5; - } else if (strcmp(pull, "NONE")==0) { - pin |= 3<<5; + + if (strcmp(pull, "DEFAULT") == 0) { + // no action + } else if (strcmp(pull, "UP") == 0) { + pin |= 1 << 5; + } else if (strcmp(pull, "DOWN") == 0) { + pin |= 2 << 5; + } else if (strcmp(pull, "NONE") == 0) { + pin |= 3 << 5; } else { printf("Error at setgpio: pull type not recognised\n"); - valid=false; + valid = false; } - - pin |= 1<<7; //board uses this pin - - if (valid) gpiomap->pins[val] = pin; + + pin |= 1 << 7; // board uses this pin + + if (valid) + gpiomap->pins[val] = pin; } - } /* DT atom related part */ - else if (strcmp(cmd, "dt_blob")==0) { + } else if (strcmp(cmd, "dt_blob") == 0) { finish_data(); - + has_dt = true; - c+=strlen("dt_blob"); - - receive_dt=true; - data_receive=true; - + c += strlen("dt_blob"); + + receive_dt = true; + data_receive = true; + data_len = 0; data_cap = 4; data = &dt_atom.data; - *data = (char *) malloc(data_cap); - - if (parse_data(c)) { + *data = (char *)malloc(data_cap); + + if (parse_data(c)) return -1; - } + continue_data = true; - - } - + /* Custom data related part */ - else if (strcmp(cmd, "custom_data")==0) { + } else if (strcmp(cmd, "custom_data") == 0) { finish_data(); - - c+=strlen("custom_data"); - + + c += strlen("custom_data"); + if (custom_cap == custom_ct) { custom_cap *= 2; - custom_atom = (struct atom_t*) realloc(custom_atom, custom_cap * sizeof(struct atom_t)); + custom_atom = (struct atom_t *)realloc(custom_atom, custom_cap * sizeof(struct atom_t)); } - - receive_dt=false; - data_receive=true; - + + receive_dt = false; + data_receive = true; + data_len = 0; data_cap = 4; data = &custom_atom[custom_ct].data; - *data = (char *) malloc(data_cap); - - if (parse_data(c)) { + *data = (char *)malloc(data_cap); + + if (parse_data(c)) return -1; - } + continue_data = true; - - } else if (strcmp(cmd, "end") ==0) { - //close last data atom - continue_data=false; - } + + } else if (strcmp(cmd, "end") == 0) { + /* close last data atom */ + continue_data = false; + /* Incoming data */ - else if (data_receive) { - if (parse_data(c)) { + } else if (data_receive) { + if (parse_data(c)) return -1; - } + continue_data = true; - } - - - if (!continue_data) finish_data(); + } + + if (!continue_data) + finish_data(); return 0; } -int read_text(char* in) { - FILE * fp; - char * line = NULL; - char * c = NULL; + +int read_text(char *in) +{ + char *line = NULL, *c = NULL; + FILE *fp; size_t len = 0; ssize_t read; - int linect = 0; - char * command = (char*) malloc (101); - int i; - + int i, linect = 0; + char *command; + + command = (char*)malloc(101); + has_dt = false; - + printf("Opening file %s for read\n", in); - + fp = fopen(in, "r"); if (fp == NULL) { printf("Error opening input file\n"); return -1; } - //allocating memory and setting up required atoms + /* allocating memory and setting up required atoms */ custom_cap = 1; - custom_atom = (struct atom_t*) malloc(sizeof(struct atom_t) * custom_cap); - - total_size=ATOM_SIZE*2+HEADER_SIZE+VENDOR_SIZE+GPIO_SIZE; - + custom_atom = (struct atom_t *)malloc(sizeof(struct atom_t) * custom_cap); + + total_size = ATOM_SIZE * 2 + HEADER_SIZE + VENDOR_SIZE + GPIO_SIZE; + vinf_atom.type = ATOM_VENDOR_TYPE; vinf_atom.count = ATOM_VENDOR_NUM; - vinf = (struct vendor_info_d *) calloc(1, sizeof(struct vendor_info_d)); + vinf = (struct vendor_info_d *)calloc(1, sizeof(struct vendor_info_d)); vinf_atom.data = (char *)vinf; vinf_atom.dlen = VENDOR_SIZE + CRC_SIZE; - + gpio_atom.type = ATOM_GPIO_TYPE; gpio_atom.count = ATOM_GPIO_NUM; - gpiomap = (struct gpio_map_d *) calloc(1, sizeof(struct gpio_map_d)); + gpiomap = (struct gpio_map_d *)calloc(1, sizeof(struct gpio_map_d)); gpio_atom.data = (char *)gpiomap; gpio_atom.dlen = GPIO_SIZE + CRC_SIZE; - + while ((read = getline(&line, &len, fp)) != -1) { linect++; c = line; - - for (i=0; i3) { - if (strcmp(argv[3], "-c")==0) { - custom_o=4; + + if (argc > 3) { + if (strcmp(argv[3], "-c") == 0) { + custom_o = 4; } else { - //DT file specified - if (dt_atom.dlen) total_size-=(ATOM_SIZE +dt_atom.dlen - CRC_SIZE); + /* DT file specified */ + if (dt_atom.dlen) + total_size -= ATOM_SIZE + dt_atom.dlen - CRC_SIZE; ret = read_dt(argv[3]); if (ret) { printf("Error reading DT file, aborting\n"); @@ -608,33 +631,34 @@ int main(int argc, char *argv[]) { } } } - - if (argc>4 && strcmp(argv[4], "-c")==0) custom_o = 5; + + if (argc > 4 && strcmp(argv[4], "-c") == 0) + custom_o = 5; if (custom_o) - for (i = custom_o; i 0) - { + while (size > 0) { bit_flag = out >> 15; /* Get next bit: */ @@ -85,31 +84,28 @@ uint16_t getcrc(char* data, unsigned int size) /* Increment bit counter: */ bits_read++; - if(bits_read > 7) - { + if (bits_read > 7) { bits_read = 0; data++; size--; } /* Cycle check: */ - if(bit_flag) + if (bit_flag) out ^= CRC16; } // item b) "push out" the last 16 bits - int i; for (i = 0; i < 16; ++i) { bit_flag = out >> 15; out <<= 1; - if(bit_flag) + if (bit_flag) out ^= CRC16; } // item c) reverse the bits - uint16_t crc = 0; i = 0x8000; - int j = 0x0001; + j = 0x0001; for (; i != 0; i >>=1, j <<= 1) { if (i & out) crc |= j; }