
So I looked it up and the code works on my b3 but not on my b2. Besides B2s have a redundant U-Boot environment so there are two positions in flash you need to read and based on the 5th byte decide which one is the active one (0: inactive; 1: active). I modified the source accordingly and tried to simplify it by loading the whole environment at once (64k on the b3, 8k on the b2 it's low enough) and reading strings from it.
Code: Select all
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
char* getkey() {
#if defined(__powerpc__) || defined(__ppc__) || defined(__PPC__)
char env[8192];
int fd = open("/dev/mtd0", O_RDONLY);
lseek(fd, 0x50000, SEEK_SET);
read(fd, env, 8192);
if ( env[4] == 0x0 ) {
lseek(fd, 0x60000, SEEK_SET);
read(fd, env, 8192);
}
char *pos = &env[5];
#else
char env[65536];
int fd = open("/dev/mtd1", O_RDONLY);
read(fd, env, 65536);
char *pos = &env[4];
#endif
close(fd);
int l = strlen(pos);
while ( l > 0 ) {
if ( strncmp(pos, "key=", 4) == 0) {
char* res = malloc(l - 3);
strcpy(res, pos + 4);
return res;
}
pos += l+1;
l = strlen(pos);
}
return "";
}
int main() {
printf("%s\n",getkey());
}