Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/boards/xm.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/boards/xm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
32 changes: 31 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
17 changes: 17 additions & 0 deletions src/uboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/uboot.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading