static void setup_serial(void) { #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE) const u32 cpuid_offset = 0x7; const u32 cpuid_length = 0x10; struct udevice *dev; int ret, i; u8 cpuid[cpuid_length]; u8 low[cpuid_length/2], high[cpuid_length/2]; char cpuid_str[cpuid_length * 2 + 1]; u64 serialno; char serialno_str[17]; /* retrieve the device */ ret = uclass_get_device_by_driver(UCLASS_MISC, DM_GET_DRIVER(rockchip_efuse), &dev); if (ret) { debug("%s: could not find efuse device\n", __func__); return; } /* read the cpu_id range from the efuses */ ret = misc_read(dev, cpuid_offset, &cpuid, sizeof(cpuid)); if (ret) { debug("%s: reading cpuid from the efuses failed\n", __func__); return; } memset(cpuid_str, 0, sizeof(cpuid_str)); for (i = 0; i < 16; i++) sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]); debug("cpuid: %s\n", cpuid_str); /* * Mix the cpuid bytes using the same rules as in * ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c */ for (i = 0; i < 8; i++) { low[i] = cpuid[1 + (i << 1)]; high[i] = cpuid[i << 1]; } serialno = crc32_no_comp(0, low, 8); serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32; snprintf(serialno_str, sizeof(serialno_str), "%016llx", serialno); env_set("cpuid#", cpuid_str); env_set("serial#", serialno_str); #endif }
static int change_ech(struct ubi_ec_hdr *hdr, uint32_t image_seq, long long ec) { uint32_t crc; /* Check the EC header */ if (be32_to_cpu(hdr->magic) != UBI_EC_HDR_MAGIC) return errmsg("bad UBI magic %#08x, should be %#08x", be32_to_cpu(hdr->magic), UBI_EC_HDR_MAGIC); crc = crc32_no_comp(UBI_CRC32_INIT, hdr, UBI_EC_HDR_SIZE_CRC); if (be32_to_cpu(hdr->hdr_crc) != crc) return errmsg("bad CRC %#08x, should be %#08x\n", crc, be32_to_cpu(hdr->hdr_crc)); hdr->image_seq = cpu_to_be32(image_seq); hdr->ec = cpu_to_be64(ec); crc = crc32_no_comp(UBI_CRC32_INIT, hdr, UBI_EC_HDR_SIZE_CRC); hdr->hdr_crc = cpu_to_be32(crc); return 0; }
static void openwrt_factory_reset() { struct jffs2_unknown_node *node; u32 i; u32 crc; printf("Searching for JFFS2 filesystem\n"); for (i = CFG_LOAD_ADDR; i < CFG_LOAD_ADDR + 0xFC0000; i++) { node = (struct jffs2_unknown_node *) i; if (node->magic == JFFS2_MAGIC_BITMASK) { crc = crc32_no_comp(0, (unsigned char *)node, sizeof(struct jffs2_unknown_node) - 4); if (crc == node->hdr_crc) { printf("JFFS2 Magic Bitmask with correct CRC at 0x%X\n", node); printf("Replacing Magic Bitmask with 0xDEADCODE...\n"); static unsigned char eof_mark[4] = {0xde, 0xad, 0xc0, 0xde}; static unsigned char *pad = eof_mark; char buffer[256]; sprintf(buffer, "erase 0x%X +0x%X; cp.b 0x%X 0x%X 0x%X", node, 4, pad, node, 4); if(bsb_run(buffer)) { blink_led(3,250); printf("Done\n"); } else { blink_led(10,100); printf("Error writing to flash\n"); } bsb_run("reset"); } } } printf("JFFS2 not found.\n"); blink_led(10,100); }
int main(int argc, char **argv) { int errors = 0; int verbose = 0; char *prog_name = argv[0]; two_pass = 1; ++argv; --argc; if (argc != 2) { usage(prog_name); return 1; } FILE *fd = fopen(argv[0], "r"); if (!fd) { perror(argv[0]); return 1; } while (1) { union jffs2_node_union node; int off = ftell(fd); if (fread(&node, 1, sizeof(node), fd) != sizeof(node)) break; if (node.u.magic == KSAMTIB_CIGAM_2SFFJ) { fprintf(stderr, "ERROR: reverse endianess detected!\n"); break; } if (node.u.magic == 0xFFFF) { if (verbose) printf("%08x: empty marker - going to next eraseblock\n", off); if (fseek(fd, (off + ES + 1) & ~ES , SEEK_SET) < 0) break; continue; } if (verbose) printf("at %08x: %04x | %04x (%lu bytes): ", off, node.u.magic, node.u.nodetype, node.u.totlen); if (crc32_no_comp(0, (unsigned char*)&node, sizeof(node.u) - 4) != node.u.hdr_crc) { ++errors; printf(" ** wrong crc **\n"); } switch (node.u.nodetype) { case JFFS2_NODETYPE_DIRENT: { fseek(fd, off + sizeof(struct jffs2_raw_dirent), SEEK_SET); char name[node.d.nsize + 1]; fread(name, node.d.nsize, 1, fd); name[node.d.nsize] = 0; if (verbose) printf("DIRENT, ino %lu (%s), parent=%lu\n", node.d.ino, name, node.d.pino); inodes[node.d.ino] = name; node_type[node.d.ino] = node.d.type; childs[node.d.pino].push_back(node.d.ino); break; } case JFFS2_NODETYPE_INODE: { if (verbose) printf("\n"); if (crc32_no_comp(0, (unsigned char*)&node.i, sizeof(struct jffs2_raw_inode) - 8) != node.i.node_crc) { errors++; printf(" ** wrong node crc **\n"); } if (verbose) { printf(" INODE, ino %lu (version %lu) at %08lx\n", node.i.ino, node.i.version, node.i.offset); printf(" compression: %d, user compression requested: %d\n", node.i.compr, node.i.usercompr); } int compr_size = node.i.csize; int uncompr_size = node.i.dsize; if (verbose) printf(" compr_size: %d, uncompr_size: %d\n", compr_size, uncompr_size); unsigned char compr[compr_size], uncomp[uncompr_size]; off_t whence = ftello(fd); fread(compr, compr_size, 1, fd); if (crc32_no_comp(0, compr, compr_size) != node.i.data_crc) { errors++; printf(" ** wrong data crc **\n"); } else { if (verbose) printf(" data crc ok\n"); if (!two_pass) { if (do_uncompress(uncomp, uncompr_size, compr, compr_size, node.i.compr) != uncompr_size) { errors++; printf(" ** data uncompress failed!\n"); } else { nodedata[node.i.ino][node.i.version] = nodedata_s(uncomp, uncompr_size, node.i.offset, node.i.isize, node.i.gid, node.i.uid, node.i.mode); } } else { nodedata[node.i.ino][node.i.version] = nodedata_s(fd, whence, uncompr_size, node.i.compr, uncompr_size, node.i.offset, node.i.isize, node.i.gid, node.i.uid, node.i.mode); } } break; } case JFFS2_NODETYPE_CLEANMARKER: if (verbose) printf("CLEANMARKER\n"); break; case JFFS2_NODETYPE_PADDING: if (verbose) printf("PADDING\n"); break; default: errors++; printf(" ** INVALID ** - nodetype %04x\n", node.u.nodetype); } if (node.u.totlen) fseek(fd, (off + node.u.totlen + 3) &~3 , SEEK_SET); else { errors++; printf(" ** INVALID NODE SIZE. skipping to next eraseblock\n"); fseek(fd, (off + ES + 1) & ~ES , SEEK_SET); } } if (errors) { if (!inodes.empty()) printf("there were errors, but some valid stuff was detected. continuing.\n"); else { fprintf(stderr, "errors present and no valid data.\n"); return 2; } } node_type[1] = DT_DIR; prefix = argv[1]; do_list(1); return 0; }
uint32_t crc32_env_ubi (uint32_t crc, const unsigned char *p, unsigned int len) { return crc32_no_comp(crc ^ 0xffffffffL, p, len) ^ 0xffffffffL; }
uint32_t ZEXPORT crc32 (uint32_t crc, const Bytef *p, uInt len) { return crc32_no_comp(crc ^ 0xffffffffL, p, len) ^ 0xffffffffL; }