xref: /aosp_15_r20/external/vboot_reference/futility/updater_dut.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2023 The ChromiumOS Authors
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  *
5  * The DUT interface helper functions for the firmware updater.
6  */
7 
8 #include <assert.h>
9 #ifdef HAVE_CROSID
10 #include <crosid.h>
11 #endif
12 #include <limits.h>
13 #include "crossystem.h"
14 #include "updater.h"
15 
dut_get_manifest_key(char ** manifest_key_out,struct updater_config * cfg)16 int dut_get_manifest_key(char **manifest_key_out, struct updater_config *cfg)
17 {
18 	if (cfg->dut_is_remote) {
19 		WARN("Cannot retrieve the remote DUT manifest info. "
20 		     "Please specify the DUT type by --model.\n");
21 		return -1;
22 	}
23 #ifdef HAVE_CROSID
24 	return crosid_get_firmware_manifest_key(manifest_key_out);
25 #else
26 	ERROR("This version of futility was compiled without libcrosid "
27 	      "(perhaps compiled outside of the Chrome OS build system?) and "
28 	      "the update command is not fully supported.  Either compile "
29 	      "from the Chrome OS build, or pass --model to manually specify "
30 	      "the machine model.\n");
31 	return -1;
32 #endif
33 }
34 
dut_set_property_string(const char * key,const char * value,struct updater_config * cfg)35 int dut_set_property_string(const char *key, const char *value,
36 			    struct updater_config *cfg)
37 {
38 	if (cfg->dut_is_remote) {
39 		WARN("Ignored setting property %s on a remote DUT.\n", key);
40 		return -1;
41 	}
42 	return VbSetSystemPropertyString(key, value);
43 }
44 
dut_get_property_string(const char * key,char * dest,size_t size,struct updater_config * cfg)45 int dut_get_property_string(const char *key, char *dest, size_t size,
46 			    struct updater_config *cfg)
47 {
48 	if (cfg->dut_is_remote) {
49 		WARN("Ignored getting property %s on a remote DUT.\n", key);
50 		return -1;
51 	}
52 	return VbGetSystemPropertyString(key, dest, size);
53 }
54 
dut_set_property_int(const char * key,const int value,struct updater_config * cfg)55 int dut_set_property_int(const char *key, const int value,
56 			 struct updater_config *cfg)
57 {
58 	if (cfg->dut_is_remote) {
59 		WARN("Ignored setting property %s on a remote DUT.\n", key);
60 		return -1;
61 	}
62 	return VbSetSystemPropertyInt(key, value);
63 }
64 
dut_get_property_int(const char * key,struct updater_config * cfg)65 int dut_get_property_int(const char *key, struct updater_config *cfg)
66 {
67 	if (cfg->dut_is_remote) {
68 		WARN("Ignored getting property %s on a remote DUT.\n", key);
69 		return -1;
70 	}
71 	return VbGetSystemPropertyInt(key);
72 }
73 
74 /* An helper function to return "mainfw_act" system property.  */
dut_get_mainfw_act(struct updater_config * cfg)75 static int dut_get_mainfw_act(struct updater_config *cfg)
76 {
77 	char buf[VB_MAX_STRING_PROPERTY];
78 
79 	if (dut_get_property_string("mainfw_act", buf, sizeof(buf), cfg) != 0)
80 		return SLOT_UNKNOWN;
81 
82 	if (strcmp(buf, FWACT_A) == 0)
83 		return SLOT_A;
84 	else if (strcmp(buf, FWACT_B) == 0)
85 		return SLOT_B;
86 
87 	return SLOT_UNKNOWN;
88 }
89 
90 /* A helper function to return the "tpm_fwver" system property. */
dut_get_tpm_fwver(struct updater_config * cfg)91 static int dut_get_tpm_fwver(struct updater_config *cfg)
92 {
93 	return dut_get_property_int("tpm_fwver", cfg);
94 }
95 
96 /* A helper function to return the "hardware write protection" status. */
dut_get_wp_hw(struct updater_config * cfg)97 static int dut_get_wp_hw(struct updater_config *cfg)
98 {
99 	/* wpsw refers to write protection 'switch', not 'software'. */
100 	return dut_get_property_int("wpsw_cur", cfg);
101 }
102 
dut_get_platform_version(struct updater_config * cfg)103 static int dut_get_platform_version(struct updater_config *cfg)
104 {
105 	long rev = dut_get_property_int("board_id", cfg);
106 	/* Assume platform version = 0 on error. */
107 	if (rev < 0)
108 		rev = 0;
109 	if (rev > INT_MAX)
110 		rev = INT_MAX;
111 	return rev;
112 }
113 
114 /* Helper function to return host software write protection status. */
dut_get_wp_sw(const char * programmer)115 static int dut_get_wp_sw(const char *programmer)
116 {
117 	assert(programmer);
118 	bool mode;
119 
120 	if (flashrom_get_wp(programmer, &mode, NULL, NULL, -1)) {
121 		/* Read WP status error */
122 		return -1;
123 	}
124 	return mode;
125 }
126 
127 /* Helper function to return host AP software write protection status. */
dut_get_wp_sw_ap(struct updater_config * cfg)128 static inline int dut_get_wp_sw_ap(struct updater_config *cfg)
129 {
130 	return dut_get_wp_sw(cfg->image.programmer);
131 }
132 
133 /* Helper function to return host EC software write protection status. */
dut_get_wp_sw_ec(struct updater_config * cfg)134 static inline int dut_get_wp_sw_ec(struct updater_config *cfg)
135 {
136 	return dut_get_wp_sw(cfg->ec_image.programmer);
137 }
138 
139 /* Helper functions to use or configure the DUT properties. */
140 
dut_get_property(enum dut_property_type property_type,struct updater_config * cfg)141 int dut_get_property(enum dut_property_type property_type,
142 		     struct updater_config *cfg)
143 {
144 	struct dut_property *prop;
145 
146 	assert(property_type < DUT_PROP_MAX);
147 	prop = &cfg->dut_properties[property_type];
148 	if (!prop->initialized) {
149 		prop->initialized = 1;
150 		prop->value = prop->getter(cfg);
151 	}
152 	return prop->value;
153 }
154 
dut_init_properties(struct dut_property * props,int num)155 void dut_init_properties(struct dut_property *props, int num)
156 {
157 	memset(props, 0, num * sizeof(*props));
158 	assert(num >= DUT_PROP_MAX);
159 	props[DUT_PROP_MAINFW_ACT].getter = dut_get_mainfw_act;
160 	props[DUT_PROP_TPM_FWVER].getter = dut_get_tpm_fwver;
161 	props[DUT_PROP_PLATFORM_VER].getter = dut_get_platform_version;
162 	props[DUT_PROP_WP_HW].getter = dut_get_wp_hw;
163 	props[DUT_PROP_WP_SW_AP].getter = dut_get_wp_sw_ap;
164 	props[DUT_PROP_WP_SW_EC].getter = dut_get_wp_sw_ec;
165 }
166