0

I'm stumped, and I need someone to tell me what I'm missing.

I've had to upgrade the U-boot bootloader on my devices, and naturally I've had to make things fit again. And right now I'm trying to get the U-Boot environment variables accessible from Linux/Android.

Long story short, I've created a file /etc/fw_env.config that points to U-Boot's env section in Flash. As documented here: https://elinux.org/U-boot_environment_variables_in_linux

This has not been successful, and so I started adding print statements to the source code to debug my device. I tracked the error down to a get_config() function, which as one could imagine, opens the /etc/fw_env.config and writes the values within to the necessary variables.

I've narrowed it further down to the sscanf() function, which returns 0, as in 0 variables read and/or written. So, as a sanity check, I isolated the function and made my own little program separately from my source code (variable names and structures I kept exactly the same).

/* sscanf example */
#include <stdio.h>

struct envdev_s {
    const char *devname;            /* Device name */
    long long devoff;               /* Device offset */
    unsigned long env_size;         /* environment size */
    unsigned long erase_size;       /* device erase size */
    unsigned long env_sectors;      /* number of environment sectors */
    unsigned int mtd_type;          /* type of the MTD device */
};

static struct envdev_s envdevices[2] = {};

#define DEVNAME(i)    envdevices[(i)].devname
#define DEVOFFSET(i)  envdevices[(i)].devoff
#define ENVSIZE(i)    envdevices[(i)].env_size
#define DEVESIZE(i)   envdevices[(i)].erase_size
#define ENVSECTORS(i) envdevices[(i)].env_sectors
#define DEVTYPE(i)    envdevices[(i)].mtd_type

int main ()
{
  char dump [] = "/dev/mtd1 0xc0000 0x2000  0x2000\n";
  char *devname;
  int i = 0;
  int rc;

  printf("I was here in get_config : dump = %s\n", dump);
  printf("I was here in get_config : i = %d\n", i);

  rc = sscanf(dump, "%ms %lli %lx %lx %lx",
                &devname,
                &DEVOFFSET(i),
                &ENVSIZE(i),
                &DEVESIZE(i),
                &ENVSECTORS(i));

  printf("I was here in get_config : rc = %d\n", rc);

  return 0;
}

Also recreated here: http://cpp.sh/5ckms

Now, when I run this independently, it works as I expect it should, particularly outputting:

I was here in get_config : rc = 4

4 being successful, as char dump [] = "/dev/mtd1 0xc0000 0x2000 0x2000\n"; But when I compile this and run it on my device, it returns:

I was here in get_config : rc = 0

Computer says NO! And no other error messages to work with.

I'm obviously missing some fundamental understanding here. Either some permissions, or some setup-variables somewhere, but I wouldn't know in where to start. Could someone please point me in the right direction?

Aaron Elliott
  • 73
  • 1
  • 8
  • Could you specify the Android version you are using? – f9c69e9781fa194211448473495534 May 02 '20 at 21:23
  • Currently, trying to build Android 6, and using a 2017 version of the bootloader from the device vendor. The above snippet of code is part of the `get_config()` function from U-Boot, and located in `fw_env.c`. Now, I've worked on this some more since posting, and I suspect the `%ms` formatter is what fails first on my device, which is why `rc = 0`. Others have had questions about `%ms` and here's a link to that discussion: https://stackoverflow.com/questions/38685724/difference-between-ms-and-s-scanf – Aaron Elliott May 03 '20 at 07:56
  • 1
    So try without `%ms` extension. – KamilCuk May 03 '20 at 08:01
  • 1
    Support for `%ms` was added in Android 9 ([link](https://android.googlesource.com/platform/bionic/+/38e4aefe6c7804d809cfe9e05619ac1a6935e565)). – f9c69e9781fa194211448473495534 May 03 '20 at 08:05
  • Thank you so much for that link! That explains a few things, did you already know of it? Or can you google well? ... Yes, I have. But it leads to segmentation fault, and this is where I have to confess that my C++ pointer-kung-fu is really lacking. And that is where I am currently stuck. – Aaron Elliott May 03 '20 at 08:32
  • Got it working, and indeed it was the `%ms`. I changed it to `%s` and allocated some memory for it. Thanks for your help. Would you like to write up a full answer to get credit? – Aaron Elliott May 03 '20 at 09:40

1 Answers1

0

For completeness, I am answering this question based on the help I have received here on StackOverflow:

As stated in the comments, %ms was not added until Android 9, and I was working on Android 6. It still did not produce any compiler errors which is particularly misleading. I ended up using %s, which worked fine.

Aaron Elliott
  • 73
  • 1
  • 8