简体   繁体   English

AOSP 和 U-Boot - sscanf() 不写入变量

[英]AOSP and U-Boot - sscanf() not writing to variables

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.我不得不在我的设备上升级 U-boot 引导加载程序,自然我不得不让一切重新适应。 And right now I'm trying to get the U-Boot environment variables accessible from Linux/Android.现在我正在尝试从 Linux/Android 访问 U-Boot 环境变量。

Long story short, I've created a file /etc/fw_env.config that points to U-Boot's env section in Flash.长话短说,我创建了一个文件/etc/fw_env.config ,它指向 Flash 中 U-Boot 的env部分。 As documented here: https://elinux.org/U-boot_environment_variables_in_linux如此处所述: 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.我将错误跟踪到get_config() function,可以想象,它打开/etc/fw_env.config并将其中的值写入必要的变量。

I've narrowed it further down to the sscanf() function, which returns 0, as in 0 variables read and/or written.我已将其进一步缩小到sscanf() function,它返回 0,就像读取和/或写入 0 个变量一样。 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).所以,作为一个健全的检查,我隔离了 function 并与我的源代码分开制作了我自己的小程序(我保持完全相同的变量名称和结构)。

/* 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也在这里重新创建: 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"; 4 成功,作为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.电脑说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?有人可以指出我正确的方向吗?

For completeness, I am answering this question based on the help I have received here on StackOverflow:为了完整起见,我根据我在 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.如评论中所述,直到 Android 9 才添加%ms ,而我正在研究 Android 6。它仍然没有产生任何编译器错误,这尤其具有误导性。 I ended up using %s , which worked fine.我最终使用了%s ,效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM