Skip to content

Commit

Permalink
CLI webreport: report only dsnapshot if esnapshot could not be gotten
Browse files Browse the repository at this point in the history
  • Loading branch information
7Ji committed May 19, 2024
1 parent c1ada55 commit c998dd0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 38 deletions.
71 changes: 49 additions & 22 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

#define CLI_LAZY_WRITE

#define CLI_WEBREPORT_URL_PARENT "https://7ji.github.io/ampart-web-reporter/?"
#define CLI_WEBREPORT_ARG_ESNAPSHOT "esnapshot="
#define CLI_WEBREPORT_ARG_DSNAPSHOT "dsnapshot="
#define CLI_WEBREPORT_URL "https://7ji.github.io/ampart-web-reporter/?%s&%s"
#define CLI_WEBREPORT_URL_MAXLEN 0x800

Expand Down Expand Up @@ -898,40 +901,64 @@ cli_mode_webreport(
struct dtb_buffer_helper const * const bhelper,
struct ept_table const * const table
) {
// The reported URL should look this:
// https://7ji.github.io/ampart-web-reporter/?esnapshot=bootloader:0:4194304:0%20reserved:37748736:67108864:0%20cache:113246208:754974720:2%20env:876609536:8388608:0%20logo:893386752:33554432:1%20recovery:935329792:33554432:1%20rsv:977272832:8388608:1%20tee:994050048:8388608:1%20crypt:1010827264:33554432:1%20misc:1052770304:33554432:1%20instaboot:1094713344:536870912:1%20boot:1639972864:33554432:1%20system:1681915904:1073741824:1%20params:2764046336:67108864:2%20bootfiles:2839543808:754974720:2%20data:3602907136:4131389440:4&dsnapshot=logo::33554432:1%20recovery::33554432:1%20rsv::8388608:1%20tee::8388608:1%20crypt::33554432:1%20misc::33554432:1%20instaboot::536870912:1%20boot::33554432:1%20system::1073741824:1%20cache::536870912:2%20params::67108864:2%20data::-1:4
char arg_dsnapshot[CLI_WEBREPORT_URL_MAXLEN];
char arg_esnapshot[CLI_WEBREPORT_URL_MAXLEN];
char url[CLI_WEBREPORT_URL_MAXLEN];
unsigned len_dsnapshot;
unsigned len_esnapshot;
unsigned len_used;
unsigned len_current;
bool has_esnapshot;

fputs("CLI mode webreport: Print a URL that can be opened in browser to get well-formatted partitio info\n", stderr);
if (!bhelper || !bhelper->dtb_count) {
fputs("CLI mode webreport: DTB not correct or invalid\n", stderr);
return 1;
}
if (!table || !table->partitions_count || ept_valid_table(table)) {
fputs("CLI mode webreport: EPT does not exist or is invalid, refuse to work\n", stderr);
return 2;
if (table && table->partitions_count && !ept_valid_table(table)) {
has_esnapshot = true;
} else {
fputs("CLI mode webreport: EPT does not exist or is invalid, web report would not contain EPT\n", stderr);
has_esnapshot = false;
}
// The reported URL should look this:
// https://7ji.github.io/ampart-web-reporter/?esnapshot=bootloader:0:4194304:0%20reserved:37748736:67108864:0%20cache:113246208:754974720:2%20env:876609536:8388608:0%20logo:893386752:33554432:1%20recovery:935329792:33554432:1%20rsv:977272832:8388608:1%20tee:994050048:8388608:1%20crypt:1010827264:33554432:1%20misc:1052770304:33554432:1%20instaboot:1094713344:536870912:1%20boot:1639972864:33554432:1%20system:1681915904:1073741824:1%20params:2764046336:67108864:2%20bootfiles:2839543808:754974720:2%20data:3602907136:4131389440:4&dsnapshot=logo::33554432:1%20recovery::33554432:1%20rsv::8388608:1%20tee::8388608:1%20crypt::33554432:1%20misc::33554432:1%20instaboot::536870912:1%20boot::33554432:1%20system::1073741824:1%20cache::536870912:2%20params::67108864:2%20data::-1:4
char arg_dsnapshot[CLI_WEBREPORT_URL_MAXLEN] = "";
char arg_esnapshot[CLI_WEBREPORT_URL_MAXLEN] = "";
unsigned len_dsnapshot = 0;
unsigned len_esnapshot = 0;

len_dsnapshot = 0;
int r = dtb_webreport(bhelper, arg_dsnapshot, &len_dsnapshot);
if (r) {
fputs("CLI mode webreport: Failed to prepare dsnapshot argument\n", stderr);
return 2 + r;
}
r = ept_webreport(table, arg_esnapshot, &len_esnapshot);
if (r) {
fputs("CLI mode webreport: Failed to prepare esnapshot argument\n", stderr);
return 6 + r;
}
char url[2048];
r = snprintf(url, CLI_WEBREPORT_URL_MAXLEN, CLI_WEBREPORT_URL, arg_dsnapshot, arg_esnapshot);
if (r >= CLI_WEBREPORT_URL_MAXLEN) {
fputs("CLI mode webreport: URL truncated\n", stderr);
return 10;
} else if (r < 0) {
fputs("CLI mode webreport: Error occured during output\n", stderr);
return 11;
len_esnapshot = 0;
if (has_esnapshot) {
r = ept_webreport(table, arg_esnapshot, &len_esnapshot);
if (r) {
fputs("CLI mode webreport: Failed to prepare esnapshot argument\n", stderr);
return 6 + r;
}
}
if ((sizeof CLI_WEBREPORT_URL_PARENT) - 1 + (has_esnapshot ? ((sizeof CLI_WEBREPORT_ARG_ESNAPSHOT) - 1 + len_esnapshot + 1): 0) + (sizeof CLI_WEBREPORT_ARG_DSNAPSHOT) + - 1 + len_dsnapshot + 1 > CLI_WEBREPORT_URL_MAXLEN) {
fputs("CLI mode webreport: result URL length would be too long, aborting\n", stderr);
return 9;
}
len_current = (sizeof CLI_WEBREPORT_URL_PARENT) - 1;
memcpy(url, CLI_WEBREPORT_URL_PARENT, len_current);
len_used = len_current;
if (has_esnapshot) {
len_current = sizeof(CLI_WEBREPORT_ARG_ESNAPSHOT) - 1;
memcpy(url + len_used, CLI_WEBREPORT_ARG_ESNAPSHOT, len_current);
len_used += len_current;
memcpy(url + len_used, arg_esnapshot, len_esnapshot);
len_used += len_esnapshot;
url[len_used++] = '&';
}
len_current = sizeof(CLI_WEBREPORT_ARG_DSNAPSHOT) - 1;
memcpy(url + len_used, CLI_WEBREPORT_ARG_DSNAPSHOT, len_current);
len_used += len_current;
memcpy(url + len_used, arg_dsnapshot, len_dsnapshot);
len_used += len_dsnapshot;
url[len_used] = '\0';
fputs("CLI mode webreport: Please copy the following URL to your browser to check the well-formatted partition info:\n", stderr);
puts(url);
return 0;
Expand Down
11 changes: 2 additions & 9 deletions src/dtb.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,12 @@
#define DTB_PARTITION_VERSION 1
#define DTB_PARTITION_MAGIC 0x00447E41U
#define DTB_WEBREPORT_ARG_MAXLEN 0x800U
#define DTB_WEBREPORT_ARG "dsnapshot="

/* Macro */

#define DTB_GET_PARTITIONS_NODE_FROM_DTS(dts, max_offset) \
dts_get_node_from_path(dts, max_offset, "/partitions", 11)

/* Variable */
size_t const
len_dtb_webreport_arg = strlen(DTB_WEBREPORT_ARG);

/* Function */

uint32_t
Expand Down Expand Up @@ -699,10 +694,8 @@ dtb_webreport(
fputs("DTB webreport: DTB invalid\n", stderr);
return 1;
}
// Initial dsnapshot= part
strncpy(arg_dsnapshot, DTB_WEBREPORT_ARG, len_dtb_webreport_arg);
char *current = arg_dsnapshot + len_dtb_webreport_arg;
uint32_t len_available = DTB_WEBREPORT_ARG_MAXLEN - len_dtb_webreport_arg;
char *current = arg_dsnapshot;
uint32_t len_available = DTB_WEBREPORT_ARG_MAXLEN;
// For each partition
struct dts_partitions_helper const *const phelper = &bhelper->dtbs->phelper;
uint32_t const pcount = util_safe_partitions_count(phelper->partitions_count);
Expand Down
9 changes: 2 additions & 7 deletions src/ept.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@

/* Variable */

uint32_t const
len_ept_webreport_arg = strlen(EPT_WEBREPORT_ARG);

uint32_t const
ept_header_version_uint32[] = {
EPT_HEADER_VERSION_UINT32_0,
Expand Down Expand Up @@ -757,10 +754,8 @@ ept_webreport(
fputs("EPT snapshot: EPT invalid\n", stderr);
return 1;
}
// Initial esnapshot= part
strncpy(arg_esnapshot, EPT_WEBREPORT_ARG, len_ept_webreport_arg);
char *current = arg_esnapshot + len_ept_webreport_arg;
uint32_t len_available = EPT_WEBREPORT_ARG_MAXLEN - len_ept_webreport_arg;
char *current = arg_esnapshot;
uint32_t len_available = EPT_WEBREPORT_ARG_MAXLEN;
// For each partition
struct ept_partition const * part;
uint32_t const pcount = util_safe_partitions_count(table->partitions_count);
Expand Down

0 comments on commit c998dd0

Please sign in to comment.