diff --git a/src/boards/xm.c b/src/boards/xm.c index 9998aa81..a9307e2c 100644 --- a/src/boards/xm.c +++ b/src/boards/xm.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -18,6 +19,7 @@ #include "firmware.h" #include "hal/hisi/hal_hisi.h" #include "tools.h" +#include "uboot.h" bool is_xm_board() { // crucial to keep detection status in memory for deal with XM flash ops @@ -232,6 +234,59 @@ static void extract_netip_creds(char username[64], char pwd[64]) { cJSON_Delete(json); } +/* The Hi3518/Hi3516C V1 Sofia firmware never probes its sensor: libdvr's + * save_dvrHWInfo() unpacks the factory HWID (U-Boot env, 16 hex digits read + * as one little-endian u64) into g_hwinfo, and Sofia brings up whatever + * sensor the build was made for. get_hwinfo(5), "forward video chip" in + * libdvr's SYSTEM INFO dump, is the high nibble of byte 2; the names are that + * dump's. Only V1 is decoded: later libdvr generations were not checked. */ +static const struct { + const char *vendor, *model, *control; +} xm_v1_hwid_sensors[] = { + {"OmniVision", "OV9712", "i2c"}, + {NULL, NULL, NULL}, /* "External Signal" */ + {"Sony", "IMX138", "spi"}, + {"Sony", "IMX104", "spi"}, + {"Sony", "IMX122", "spi"}, + {"Sony", "IMX136", "spi"}, + {"ON Semiconductor", "MT9P006", "i2c"}, + {"ON Semiconductor", "AR0130", "i2c"}, + {"ON Semiconductor", "AR0140", "i2c"}, + {"Silicon Optronics", "H22", "i2c"}, + {"ON Semiconductor", "AR0141", "i2c"}, + {"BYD", "BF3116", "i2c"}, + {"PrimeSensor", "PS1210K", "i2c"}, +}; + +bool xm_hwid_sensor(const char **vendor, const char **model, + const char **control) { + if (!is_xm_board() || !getchipname() || chip_generation != HISI_V1) + return false; + + const char *hwid = uboot_getenv("HWID"); + if (!hwid) + return false; + + /* save_dvrHWInfo() writes all 16 digits; anything shorter (HWID=0, a + * hand-set value) would otherwise decode as sensor 0, the OV9712. */ + if (strlen(hwid) != 16) + return false; + for (int i = 0; i < 16; i++) + if (!isxdigit((unsigned char)hwid[i])) + return false; + unsigned long long v = strtoull(hwid, NULL, 16); + + unsigned idx = (v >> 20) & 0xf; + if (idx >= sizeof(xm_v1_hwid_sensors) / sizeof(*xm_v1_hwid_sensors) || + !xm_v1_hwid_sensors[idx].model) + return false; + + *vendor = xm_v1_hwid_sensors[idx].vendor; + *model = xm_v1_hwid_sensors[idx].model; + *control = xm_v1_hwid_sensors[idx].control; + return true; +} + bool gather_xm_board_info(cJSON *j_inner) { char username[64] = {0}, password[64] = {0}; extract_netip_creds(username, password); diff --git a/src/boards/xm.h b/src/boards/xm.h index 94e995c3..aa3be67f 100644 --- a/src/boards/xm.h +++ b/src/boards/xm.h @@ -7,6 +7,8 @@ bool gather_xm_board_info(cJSON *j_inner); bool is_xm_board(); +bool xm_hwid_sensor(const char **vendor, const char **model, + const char **control); bool xm_spiflash_unlock_and_erase(int fd, uint32_t offset, uint32_t size); bool xm_flash_init(int fd); bool xm_kill_stuff(bool force); diff --git a/src/main.c b/src/main.c index 32781d87..78c33322 100644 --- a/src/main.c +++ b/src/main.c @@ -43,6 +43,7 @@ #include "boards/buildroot.h" #include "boards/common.h" +#include "boards/xm.h" #define RESET_CL "\x1b[0m" #define FG_RED "\x1b[31m" @@ -160,6 +161,30 @@ void add_yaml_fragment(cJSON *root, const char *key, cJSON *json) { cJSON_AddItemToObject(root, key, json); } +/* A V1 XM board whose sensor bus gives nothing back still names its sensor in + * the factory HWID, which is what the vendor firmware itself goes by. Marked + * as such: nothing was read from the part. */ +static cJSON *detect_sensors_or_hwid() { + cJSON *j = detect_sensors(); + if (j) + return j; + + const char *vendor, *model, *control; + if (!xm_hwid_sensor(&vendor, &model, &control)) + return NULL; + + j = cJSON_CreateArray(); + cJSON *j_sensor = cJSON_CreateObject(); + cJSON_AddItemToArray(j, j_sensor); + cJSON_AddStringToObject(j_sensor, "vendor", vendor); + cJSON_AddStringToObject(j_sensor, "model", model); + cJSON *j_control = cJSON_CreateObject(); + cJSON_AddItemToObject(j_sensor, "control", j_control); + cJSON_AddStringToObject(j_control, "type", control); + cJSON_AddStringToObject(j_sensor, "source", "xm-hwid"); + return j; +} + static cJSON *build_yaml() { if (!getchipname()) { explain_unknown_chip(); @@ -173,7 +198,7 @@ static cJSON *build_yaml() { add_yaml_fragment(root, "rom", get_mtd_info()); add_yaml_fragment(root, "ram", detect_ram()); add_yaml_fragment(root, "firmware", detect_firmare()); - add_yaml_fragment(root, "sensors", detect_sensors()); + add_yaml_fragment(root, "sensors", detect_sensors_or_hwid()); add_yaml_fragment(root, "clocks", clocks_build_json(true)); return root; @@ -284,6 +309,11 @@ int main(int argc, char *argv[]) { case '2': case 's': { const char *sensor = getsensoridentity(); + const char *vendor, *model, *control; + if (!sensor && xm_hwid_sensor(&vendor, &model, &control)) { + printf("%s_%s\n", model, control); + return EXIT_SUCCESS; + } if (!sensor) { if (!getchipname()) explain_unknown_chip(); diff --git a/src/uboot.c b/src/uboot.c index 2b464a2c..2fdcee3e 100644 --- a/src/uboot.c +++ b/src/uboot.c @@ -198,6 +198,7 @@ static void uboot_setenv_cb(int mtd, uint32_t offset, const char *env, enum { OP_PRINTENV = 0, OP_SETENV, + OP_LOAD, }; typedef struct { @@ -227,6 +228,9 @@ static bool cb_uboot_env(int i, const char *name, struct mtd_info_user *mtd, uboot_setenv_cb(i, u_off, addr + u_off, c->key, c->value, mtd->erasesize, c->fop); break; + case OP_LOAD: + uboot_copyenv_int(addr + u_off); + break; } close(fd); return false; @@ -242,6 +246,19 @@ char *uboot_fullenv(size_t *len) { return uenv; } +/* uboot_env_get_param() only sees an environment some earlier MTD walk copied + * in (the report's rom scan does). This one fetches it itself, for callers + * such as `ipctool -s` that never walk the flash. */ +const char *uboot_getenv(const char *name) { + if (!uenv) { + ctx_uboot_t ctx = { + .op = OP_LOAD, + }; + enum_mtd_info(&ctx, cb_uboot_env); + } + return uboot_env_get_param(name); +} + int cmd_printenv() { ctx_uboot_t ctx = { .op = OP_PRINTENV, diff --git a/src/uboot.h b/src/uboot.h index 088e6e00..99a797b1 100644 --- a/src/uboot.h +++ b/src/uboot.h @@ -7,6 +7,7 @@ int uboot_detect_env(void *buf, size_t size, size_t erasesize); const char *uboot_env_get_param(const char *name); +const char *uboot_getenv(const char *name); void uboot_copyenv_int(const void *buf); char *uboot_fullenv(size_t *len);