[英]AOSP and U-Boot - sscanf() not writing to variables
我很難過,我需要有人告訴我我錯過了什么。
我不得不在我的設備上升級 U-boot 引導加載程序,自然我不得不讓一切重新適應。 現在我正在嘗試從 Linux/Android 訪問 U-Boot 環境變量。
長話短說,我創建了一個文件/etc/fw_env.config
,它指向 Flash 中 U-Boot 的env
部分。 如此處所述: https://elinux.org/U-boot_environment_variables_in_linux
這沒有成功,所以我開始在源代碼中添加打印語句來調試我的設備。 我將錯誤跟蹤到get_config()
function,可以想象,它打開/etc/fw_env.config
並將其中的值寫入必要的變量。
我已將其進一步縮小到sscanf()
function,它返回 0,就像讀取和/或寫入 0 個變量一樣。 所以,作為一個健全的檢查,我隔離了 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;
}
也在這里重新創建: http://cpp.sh/5ckms
現在,當我獨立運行它時,它會按預期工作,尤其是輸出:
I was here in get_config : rc = 4
4 成功,作為char dump [] = "/dev/mtd1 0xc0000 0x2000 0x2000\n";
但是當我編譯它並在我的設備上運行它時,它會返回:
I was here in get_config : rc = 0
電腦說NO。 並且沒有其他錯誤消息可以使用。
我顯然在這里缺少一些基本的理解。 要么是一些權限,要么是某個地方的一些設置變量,但我不知道從哪里開始。 有人可以指出我正確的方向嗎?
為了完整起見,我根據我在 StackOverflow 上收到的幫助來回答這個問題:
如評論中所述,直到 Android 9 才添加%ms
,而我正在研究 Android 6。它仍然沒有產生任何編譯器錯誤,這尤其具有誤導性。 我最終使用了%s
,效果很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.