/* mid-level functions */ int8_t noinline ow_read_rom(ow_rom_code_t *rom) { #if ONEWIRE_BUSCOUNT > 1 uint8_t busmask = 1 << (ONEWIRE_STARTPIN); // FIXME: currently only on 1st bus #else uint8_t busmask = ONEWIRE_BUSMASK; #endif /* reset the bus */ if (!reset_onewire(busmask)) return -1; /* transmit command byte */ ow_write_byte(busmask, OW_ROM_READ_ROM); /* read 64bit rom code */ for (uint8_t i = 0; i < 8; i++) { /* read byte */ rom->bytewise[i] = ow_read_byte(busmask); } /* check CRC (last byte) */ if (rom->crc != crc_checksum(rom->bytewise, 7)) return -2; return 1; }
int8_t ow_temp_read_scratchpad(ow_rom_code_t *rom, ow_temp_scratchpad_t *scratchpad) { uint8_t busmask; int8_t ret; if (rom == NULL) ret = ow_skip_rom(); else { /* check for known family code */ if (!ow_temp_sensor(rom)) return -3; ret = ow_match_rom(rom); } if (ret < 0) return ret; /* transmit command byte */ ow_write_byte(ONEWIRE_BUSMASK, OW_FUNC_READ_SP); #if ONEWIRE_BUSCOUNT > 1 for (uint8_t bus = 0; bus < ONEWIRE_BUSCOUNT; bus++) { /* read 9 bytes from each onewire bus */ busmask = (uint8_t)(1 << (bus + ONEWIRE_STARTPIN)); #else busmask = ONEWIRE_BUSMASK; #endif for (uint8_t i = 0; i < 9; i++) { scratchpad->bytewise[i] = ow_read_byte(busmask); } /* check CRC (last byte) */ if (scratchpad->crc == crc_checksum(&scratchpad->bytewise, 8)) { /* return if we got a valid response from one device */ return 1; } #if ONEWIRE_BUSCOUNT > 1 } #endif return -2; }
/* Checks CRC after applying the mask for the given number of ports. * * The bits buffer size must be at least 40 bytes. * * Returns 0 if the data is correct, -1 otherwise */ uint32_t pbch_crc_check(pbch_t *q, char *bits, uint32_t nof_ports) { char data[40]; memcpy(data, bits, 40 * sizeof(char)); crc_set_mask(data, nof_ports); int ret = crc_checksum(&q->crc, data, 40); if (ret == 0) { uint32_t chkzeros=0; for (int i=0;i<24 && !chkzeros;i++) { chkzeros += data[i]; } if (chkzeros) { return 0; } else { return -1; } } else { return ret; } }
int main(int argc, char **argv) { int i; char *data; unsigned int crc_word, expected_word; crc_t crc_p; parse_args(argc, argv); data = malloc(sizeof(char) * (num_bits + crc_length * 2)); if (!data) { perror("malloc"); exit(-1); } if (!seed) { seed = time(NULL); } srand(seed); // Generate data for (i = 0; i < num_bits; i++) { data[i] = rand() % 2; } //Initialize CRC params and tables if (crc_init(&crc_p, crc_poly, crc_length)) { exit(-1); } // generate CRC word crc_word = crc_checksum(&crc_p, data, num_bits); free(data); // check if generated word is as expected if (get_expected_word(num_bits, crc_length, crc_poly, seed, &expected_word)) { fprintf(stderr, "Test parameters not defined in test_results.h\n"); exit(-1); } exit(expected_word != crc_word); }
int8_t eeprom_save_config(void *mac, void *ip, void *netmask, void *gateway) /* {{{ */ { (void) ip; (void) netmask; (void) gateway; /* save new ip addresses */ struct eeprom_config_base_t cfg_base; /* the eeprom section must contain valid data, if any parameter is NULL */ eeprom_read_block(&cfg_base, EEPROM_CONFIG_BASE, sizeof(struct eeprom_config_base_t)); if (mac != NULL) memcpy(&cfg_base.mac, mac, 6); #if (!UIP_CONF_IPV6 && (!defined(BOOTP_SUPPORT) \ || defined(BOOTP_TO_EEPROM_SUPPORT))) \ || defined(OPENVPN_SUPPORT) || defined(IPV6_STATIC_SUPPORT) if (ip != NULL) memcpy(&cfg_base.ip, ip, IPADDR_LEN); #endif #if !UIP_CONF_IPV6 && (!defined(BOOTP_SUPPORT) \ || defined(BOOTP_TO_EEPROM_SUPPORT)) if (netmask != NULL) memcpy(&cfg_base.netmask, netmask, 4); if (gateway != NULL) memcpy(&cfg_base.gateway, gateway, 4); #endif /* not UIP_CONF_IPV6 and (not BOOTP or BOOTP_TO_EEPROM) */ /* calculate new checksum */ uint8_t checksum = crc_checksum(&cfg_base, sizeof(struct eeprom_config_base_t) - 1); cfg_base.crc = checksum; /* save config */ eeprom_write_block(EEPROM_CONFIG_BASE, &cfg_base, sizeof(struct eeprom_config_base_t)); return 0; } /* }}} */
int8_t eeprom_save_config_ext(struct eeprom_config_ext_t *new_cfg) /* {{{ */ { /* save new ip addresses */ struct eeprom_config_ext_t cfg_ext; /* the eeprom section must contain valid data, if any parameter is NULL */ eeprom_read_block(&cfg_ext, EEPROM_CONFIG_EXT, sizeof(struct eeprom_config_ext_t)); #if defined(DNS_SUPPORT) && (!defined(BOOTP_SUPPORT) \ || defined(BOOTP_TO_EEPROM_SUPPORT)) if (new_cfg->dns_server != NULL) memcpy(&cfg_ext.dns_server, new_cfg->dns_server, IPADDR_LEN); #endif #if defined(USART_SUPPORT) if (new_cfg->usart_baudrate != 0) cfg_ext.usart_baudrate = new_cfg->usart_baudrate; #endif #if defined(HTTPD_AUTH_SUPPORT) if (new_cfg->httpd_auth_password[0] != 0) { memcpy(cfg_ext.httpd_auth_password, new_cfg->httpd_auth_password, sizeof(new_cfg->httpd_auth_password) - 1); cfg_ext.httpd_auth_password[sizeof(new_cfg->httpd_auth_password) - 1] = 0; } #endif /* calculate new checksum */ uint8_t checksum = crc_checksum(&cfg_ext, sizeof(struct eeprom_config_ext_t) - 1); cfg_ext.crc = checksum; /* save config */ eeprom_write_block(EEPROM_CONFIG_EXT, &cfg_ext, sizeof(struct eeprom_config_ext_t)); return 0; } /* }}} */
int unpack(int szb_path,szb_header_t *szb_header,image_node *header) { unsigned int i; image_node *prefix=header; for (i = 0; i <szb_header->imagecount; ++i) { if (memcmp(prefix->data->partname,"bootloader",10)==0) { printf("start unpack uboot.bin......\n"); crc_checksum(szb_path,prefix,"uboot.img"); unpack_image(szb_path,prefix,"uboot.bin"); prefix=prefix->next; continue; } if (memcmp(prefix->data->partname,"boot",4)==0) { printf("start unpack boot.img......\n"); crc_checksum(szb_path,prefix,"boot.img"); unpack_image(szb_path,prefix,"boot.img"); prefix=prefix->next; continue; } if (memcmp(prefix->data->partname,"recovery",8)==0) { printf("start unpack recovery.img......\n"); crc_checksum(szb_path,prefix,"recovery.img"); unpack_image(szb_path,prefix,"recovery.img"); prefix=prefix->next; continue; } if (memcmp(prefix->data->partname,"system",6)==0) { printf("start unpack system.img\n"); crc_checksum(szb_path,prefix,"system.img"); unpack_image(szb_path,prefix,"system.img"); prefix=prefix->next; continue; } if (memcmp(prefix->data->partname,"cpimage",7)==0) { printf("start unpack cpimage.img ......\n"); crc_checksum(szb_path,prefix,"cpimage.img"); unpack_image(szb_path,prefix,"cpimage.img"); prefix=prefix->next; continue; } if (memcmp(prefix->data->partname,"preload",7)==0) { printf("start unpack preload.img......\n"); crc_checksum(szb_path,prefix,"preload.img"); unpack_image(szb_path,prefix,"preload.img"); prefix=prefix->next; continue; } if (memcmp(prefix->data->partname,"userdata",8)==0) { printf("start analysis userdata.img......\n"); unpack_image(szb_path,prefix,"userdata.img"); prefix=prefix->next; continue; } if (memcmp(prefix->data->partname,"cache",5)==0) { printf("start analysis cache.img...... \n"); unpack_image(szb_path,prefix,"cache.img"); prefix=prefix->next; continue; } } return 0; }
/* Checks CRC after applying the mask for the given number of ports. * * The bits buffer size must be at least 40 bytes. * * Returns 0 if the data is correct, -1 otherwise */ uint32_t pbch_crc_check(pbch_t *q, char *bits, uint32_t nof_ports) { char data[40]; memcpy(data, bits, 40 * sizeof(char)); crc_set_mask(data, nof_ports); return crc_checksum(&q->crc, data, 40); }