Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* dieid.c:
- * Tool to get die ID from Gumstix and to generate random strings from it.
- *
- * Copyright (C) 2013 Jason Cipriani, jason.cipriani@gmail.com
- *
- * If redistributed in source form please include this notice and the entire
- * source. If source is modified, a note must be added identifying the
- * author of the modifications.
- *
- * If redistributed in binary form, no attribution is required.
- *
- * This program can be used for any purpose. It is provided as-is and the
- * author is not responsible for any damage resulting from the use of this
- * software.
- *
- * Tested only on Gumstix Overo WaterSTORM COM.
- */
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/mman.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <unistd.h>
- /* these may or may not be appropriate for all platforms... */
- #define DIEID_OFFSET ((off_t)0x4830A218)
- #define DIEID_LENGTH ((size_t)16)
- /**
- * read dieid from /dev/mem.
- * @param dieid buffer to store result on success.
- * @return 0 on success, non-zero on error. prints message stderr on error.
- */
- int read_dieid (unsigned char dieid[DIEID_LENGTH]) {
- int f, n;
- size_t pagesize, length;
- off_t start, dieaddr;
- void *m;
- if ((pagesize = sysconf(_SC_PAGESIZE)) == (size_t)-1) {
- perror("sysconf");
- return 1;
- }
- /* calculate page-aligned start and min # of pages required to get the id. */
- start = pagesize * (off_t)(DIEID_OFFSET / pagesize);
- dieaddr = DIEID_OFFSET - start;
- length = pagesize * (off_t)(1 + (DIEID_LENGTH - 1) / pagesize);
- if ((f = open("/dev/mem", O_RDONLY)) < 0) {
- perror("/dev/mem");
- return 1;
- }
- if ((m = mmap(NULL, length, PROT_READ, MAP_SHARED, f, start)) == (void *)-1) {
- perror("mmap");
- close(f);
- return 1;
- }
- /* bytes in memory are reversed from what u-boot prints on boot. */
- for (n = 0; n < (int)DIEID_LENGTH; ++ n)
- dieid[n] = ((const unsigned char *)m)[dieaddr + DIEID_LENGTH - n - 1];
- munmap(m, length);
- close(f);
- return 0;
- }
- /**
- * replace all ^ in template with random hex digit and print it. we use libc's
- * srand/rand. for libc independence or consistency, implement your own.
- * @param dieid die id, used as random seed.
- * @param template template string.
- */
- void gen_template (const unsigned char dieid[DIEID_LENGTH], const char *template, int salt) {
- static const char hex[16] = "0123456789abcdef";
- unsigned long seed = 0, value = 0;
- int n;
- for (n = 0; n < (int)DIEID_LENGTH - 4; n += 4)
- seed += *(unsigned long *)(dieid + n);
- srand(seed + salt);
- while (*template) {
- if (*template == '^') {
- if (value == 0)
- value = rand();
- putchar(hex[value & 15]);
- value >>= 4;
- } else {
- putchar(*template);
- }
- ++ template;
- }
- putchar('\n');
- }
- int main (int argc, char **argv) {
- int salt = 0;
- const char *template = NULL;
- unsigned char dieid[DIEID_LENGTH];
- /** very basic arg parse. */
- if ((argc == 3 || argc == 4) && !strcmp(argv[1], "-m")) {
- template = argv[2];
- salt = (argc > 3) ? atoi(argv[3]) : 0;
- } else if (argc != 1) {
- fprintf(stderr, "usage: %s [ -m template [ salt ] ]\n\n", argv[0]);
- fprintf(stderr, " no options: prints die id.\n");
- fprintf(stderr, " -m template: replace all '^' in template with random hex digit.\n");
- fprintf(stderr, " salt is an integer to produce different random output.\n\n");
- fprintf(stderr, "example: generate mac address with prefix:\n");
- fprintf(stderr, " %s -m ab:cd:ef:^^:^^:^^\n\n", argv[0]);
- fprintf(stderr, "example: generate two random macs:\n");
- fprintf(stderr, " %s -m ^^:^^:^^:^^:^^:^^ 1\n", argv[0]);
- fprintf(stderr, " %s -m ^^:^^:^^:^^:^^:^^ 2\n\n", argv[0]);
- return 1;
- }
- if (read_dieid(dieid))
- return 1;
- if (template)
- gen_template(dieid, template, salt);
- else {
- int n;
- for (n = 0; n < (int)DIEID_LENGTH; ++ n)
- printf("%02x", (unsigned)dieid[n]);
- putchar('\n');
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement