mosys: Remove device-tree config support

With Fizz/Coral migrations complete, this deletes legacy support for
mosys device-tree based cros_config impl.

BUG=b:119104647
TEST=emerge-coral mosys and local coral test
CQ-DEPEND=CL:1319909, CL:*709755, CL:*709786

Change-Id: I2888fe2f89ec00d64d94d4ce2dbca536961ab3ef
Reviewed-on: https://chromium-review.googlesource.com/1319970
Commit-Ready: C Shapiro <shapiroc@chromium.org>
Tested-by: C Shapiro <shapiroc@chromium.org>
Reviewed-by: Gregory Meinke <gmeinke@chromium.org>
diff --git a/README b/README
index 57da29b..f104926 100644
--- a/README
+++ b/README
@@ -48,14 +48,12 @@
   CC=clang meson -Darch=amd64 -Duse_cros_config=true output_dir
   ninja -C output_dir
 
-To build with use_cros_config=true, you need two files that are dynamically
+To build with use_cros_config=true, you need one file that is dynamically
 provided as inputs to the build process from other ebuilds in the Chrome OS
 build system:
-  lib/cros_config/config.dtb
   lib/cros_config/cros_config_data.c
 
 If you just want to compile something quickly, you can do:
-  touch lib/cros_config/config.dtb
   cp lib/cros_config/dummy_cros_config_data.c lib/cros_config/cros_config_data.c
 
 and then run the `meson` command as above.
diff --git a/include/lib/cros_config.h b/include/lib/cros_config.h
index f3e986a..24884b4 100644
--- a/include/lib/cros_config.h
+++ b/include/lib/cros_config.h
@@ -115,26 +115,6 @@
 			             struct sku_info *sku_info);
 
 /**
- * cros_config_setup_sku() - read SKU information (internal functions)
- *
- * Read information about a particular SKU ID. Only use this directly for
- * testing.
- *
- * @fdt: Device tree file
- * @sku_info: Returns SKU information on success
- * @find_smbios_name: SMBIOS name to look up
- * @find_wl_name: Whitelabel name to look up (this is either a model name or a
- *    whitelabel tag depending on the schema used)
- * @find_sku_id: SKU ID to look up
- * @return: 0 if OK, -ENOENT if @find_platform_names does not contain the SMBIOS
- * name for this device, or -1 for any other error
- */
-int cros_config_setup_sku(const char *fdt, struct sku_info *sku_info,
-			  const char *find_smbios_name, int find_sku_id,
-			  const char *find_wl_name,
-			  const char **platform_namep);
-
-/**
  * cros_config_smbios_platform_name_match() - shallow match on sbmios name
  *
  * @intf: Platform information, used to access SMBIOS name and SKU ID
diff --git a/lib/cros_config/config.dtb.S b/lib/cros_config/config.dtb.S
deleted file mode 100644
index 6983dae..0000000
--- a/lib/cros_config/config.dtb.S
+++ /dev/null
@@ -1,11 +0,0 @@
-#if defined(__linux__) && defined(__ELF__)
-.section .note.GNU-stack,"",%progbits
-#endif
-.section .dtb.init.rodata,"a"
-.balign 16
-.global __dtb_config_begin
-__dtb_config_begin:
-.incbin "lib/cros_config/config.dtb"
-__dtb_config_end:
-.global __dtb_config_end
-.balign 16
diff --git a/lib/cros_config/cros_config.c b/lib/cros_config/cros_config.c
index c466b33..7bd600a 100644
--- a/lib/cros_config/cros_config.c
+++ b/lib/cros_config/cros_config.c
@@ -49,223 +49,6 @@
 #include "lib/smbios.h"
 #include "lib/string.h"
 
-static int cros_config_fdt_err(const char *where, int err)
-{
-	lprintf(LOG_ERR, "%s: %s: %s\n", __func__, where, fdt_strerror(err));
-
-	return -1;
-}
-
-/**
- * check_sku_map() - Check a single sku-map node for a match
- *
- * This searches the given sku-map node to see if it is a match for the given
- * SKU ID.
- *
- * @fdt: Device tree blob
- * @node: 'sku-map' node to examine
- * @find_sku_id: SKU ID to search for. This is not required (can be -1) for
- *	single-sku matching
- * @find_smbios_name: SMBIOS name to search for. Can be NULL if the name does
- *	not need to be checked (no smbios-name-match property). This only works
- *	on x86 devices.
- * @platform_namep: Returns platform name for this SKU, if found
- * @return phandle found (> 0), if any, 0 if not found, negative on error
- */
-static int check_sku_map(const char *fdt, int node,
-			 const char *find_smbios_name, int find_sku_id,
-			 const char **platform_namep)
-{
-	const fdt32_t *data, *end, *ptr;
-	const char *smbios_name;
-	int found_phandle = 0;
-	int len;
-
-	lprintf(LOG_DEBUG, "%s: Trying %s\n", __func__,
-		fdt_get_name(fdt, node, NULL));
-	smbios_name = fdt_getprop(fdt, node, "smbios-name-match", NULL);
-	if (smbios_name &&
-	    (!find_smbios_name || strcmp(smbios_name, find_smbios_name))) {
-		lprintf(LOG_DEBUG, "%s: SMBIOS name '%s' does not match '%s'\n",
-			__func__, smbios_name,
-			find_smbios_name ? find_smbios_name : "(null)");
-		return 0;
-	}
-
-	/* If we have a single SKU, deal with that first */
-	data = fdt_getprop(fdt, node, "single-sku", &len);
-	if (data) {
-		if (len != sizeof(fdt32_t)) {
-			lprintf(LOG_ERR, "%s: single-sku: Invalid length %d\n",
-				__func__, len);
-			return -1;
-		}
-		found_phandle = fdt32_to_cpu(*data);
-		lprintf(LOG_DEBUG, "%s: Single SKU match\n", __func__);
-		goto found;
-	}
-
-	/*
-	 * Locate the map and make sure it is a multiple of 2 cells (first is
-	 * SKU ID, second is phandle).
-	 */
-	data = fdt_getprop(fdt, node, "simple-sku-map", &len);
-	if (!data)
-		return cros_config_fdt_err("find simple-sku-map", len);
-	if (len % (sizeof(fdt32_t) * 2)) {
-		/*
-		 * Validation of configuration should catch this, so this
-		 * should never happen. But we don't want to crash.
-		 */
-		lprintf(LOG_ERR, "%s: %s: simple-sku-map: Invalid length %d\n",
-			__func__, fdt_get_name(fdt, node, NULL), len);
-		return -1;
-	}
-
-	/* Search for the SKU ID in the list */
-	for (end = (fdt32_t *)((char *)data + len), ptr = data; ptr < end;
-	     ptr += 2) {
-		int sku_id = fdt32_to_cpu(ptr[0]);
-		int phandle = fdt32_to_cpu(ptr[1]);
-
-		if (sku_id == find_sku_id) {
-			found_phandle = phandle;
-			break;
-		}
-	}
-	if (!found_phandle) {
-		lprintf(LOG_DEBUG, "%s: SKU ID %d not found in mapping\n",
-			__func__, find_sku_id);
-		return 0;
-	}
-	lprintf(LOG_DEBUG, "%s: Simple SKU map match\n", __func__);
-found:
-	*platform_namep = fdt_getprop(fdt, node, "platform-name", NULL);
-	if (!*platform_namep)
-		*platform_namep = "unknown";
-	lprintf(LOG_DEBUG, "%s: Platform name '%s'\n", __func__,
-		*platform_namep);
-
-	return found_phandle;
-}
-
-/**
- * check_sku_map() - Check all sku-map nodes for a match
- *
- * This searches all the sku-map subnodes to see if it is a match for the given
- * SKU ID.
- *
- * @fdt: Device tree blob
- * @mapping_node: 'mapping' node to examine
- * @find_smbios_name: SMBIOS name to search for
- * @find_sku_id: SKU ID to search for
- * @platform_namep: Returns platform name for this SKU, if found
- * @return phandle found (> 0), if any, 0 if not found, negative on error
- */
-static int check_sku_maps(const char *fdt, int mapping_node,
-			  const char *find_smbios_name, int find_sku_id,
-			  const char **platform_namep)
-{
-	int subnode, phandle;
-
-	fdt_for_each_subnode(subnode, fdt, mapping_node)
-	{
-		phandle = check_sku_map(fdt, subnode, find_smbios_name,
-					find_sku_id, platform_namep);
-		if (phandle < 0)
-			return -1;
-		else if (phandle > 0) {
-			break;
-		}
-	}
-
-	return phandle;
-}
-
-/**
- * follow_phandle() - Find the model node pointed to by a phandle
- *
- * @fdt: Device tree blob
- * @phandle: Phandle to look up
- * @targetp: Returns the target node of the phandle
- * @return model node for this phandle, or negative on error
- */
-static int follow_phandle(const char *fdt, int phandle, int *targetp)
-{
-	const char *parent_name;
-	int node, parent;
-	int model_node;
-
-	/* Follow the phandle to the target */
-	node = fdt_node_offset_by_phandle(fdt, phandle);
-	if (node < 0)
-		return cros_config_fdt_err("find phandle for sku ID", node);
-
-	/* Figure out whether the target is a model or a sub-model */
-	parent = fdt_parent_offset(fdt, node);
-	if (parent < 0) {
-		return cros_config_fdt_err("find parent of phandle target",
-					   node);
-	}
-	parent_name = fdt_get_name(fdt, parent, NULL);
-	if (!strcmp(parent_name, "submodels")) {
-		model_node = fdt_parent_offset(fdt, parent);
-		if (model_node < 0) {
-			return cros_config_fdt_err("find sub-model parent",
-						   node);
-		}
-	} else if (!strcmp(parent_name, "models")) {
-		model_node = node;
-	} else {
-		lprintf(LOG_ERR, "%s: phandle target parent '%s' invalid\n",
-			__func__, parent_name);
-		return -1;
-	}
-	*targetp = node;
-
-	return model_node;
-}
-
-/**
- * cros_config_lookup_whitelabel() - Look up whitelabel information
- *
- * This checks whether the model is a zero-touch whitelabel and if so, checks
- * the VPD for the correct whitelabel name.
- *
- * @fdt: Device tree blob
- * @model_nodep: On entry this is the model node to be checked.
- * @find_wl_name: Whitelabel name to search for. If NULL then the value is read
- *    from the VPD using sku_get_whitelabel_from_vpd()
- * @return -1 on error, or the whitelabel tag node offset (>0) if this is a
- *    whitelabel, or 0 on lookup failure.
- */
-static int cros_config_lookup_whitelabel(const char *fdt, int model_nodep,
-					 const char *find_wl_name)
-{
-	int firmware_node;
-	int wl_tags_node;
-	int wl_tag = 0;
-
-	firmware_node = fdt_subnode_offset(fdt, model_nodep, "firmware");
-
-	wl_tags_node = fdt_subnode_offset(fdt, model_nodep, "whitelabels");
-	if (wl_tags_node >= 0) {
-		if (!find_wl_name)
-			find_wl_name = sku_get_whitelabel_from_vpd();
-		wl_tag = fdt_subnode_offset(fdt, wl_tags_node, find_wl_name);
-		if (wl_tag < 0) {
-			lprintf(LOG_ERR,
-				"Cannot find whitelabel tag '%s' for model "
-				"'%s': %s (check VPD customization ID)\n",
-				find_wl_name,
-				fdt_get_name(fdt, model_nodep, NULL),
-				fdt_strerror(wl_tag));
-			return 0;
-		}
-	}
-
-	return wl_tag;
-}
 
 #ifdef CONFIG_PLATFORM_ARCH_X86
 /**
@@ -289,82 +72,6 @@
 }
 #endif // CONFIG_PLATFORM_ARCH_X86
 
-int cros_config_setup_sku(const char *fdt, struct sku_info *sku_info,
-			  const char *find_smbios_name, int find_sku_id,
-			  const char *find_wl_name, const char **platform_namep)
-{
-	int mapping_node, model_node;
-	int wl_tag_node;
-	int phandle;
-	int target;
-	char *customization;
-
-	mapping_node = fdt_path_offset(fdt, "/chromeos/family/mapping");
-	if (mapping_node < 0)
-		return cros_config_fdt_err("find mapping", mapping_node);
-
-	phandle = check_sku_maps(fdt, mapping_node, find_smbios_name,
-				 find_sku_id, platform_namep);
-	if (phandle <= 0)
-		goto err;
-	model_node = follow_phandle(fdt, phandle, &target);
-	if (model_node < 0)
-		goto err;
-
-	/* We found the model node, so pull out the data */
-	memset(sku_info, '\0', sizeof(*sku_info));
-
-	/*
-	 * If this is a whitelabel model, select the correct model or
-	 * whitelabel tag.
-	 */
-	wl_tag_node =
-	    cros_config_lookup_whitelabel(fdt, model_node, find_wl_name);
-	if (wl_tag_node < 0) {
-		goto err;
-	} else if (wl_tag_node) {
-		/* Whitelabel info is in whitelabels table. */
-		char *sig_id_gen = (char *)mosys_malloc(128);
-
-		if (!sig_id_gen) {
-			lprintf(LOG_ERR,
-				"Could not allocate sig_id_gen string\n");
-			return -ENOMEM;
-		}
-
-		snprintf(sig_id_gen, 128, "%s-%s",
-			 fdt_get_name(fdt, model_node, NULL),
-			 fdt_get_name(fdt, wl_tag_node, NULL));
-		sku_info->signature_id = sig_id_gen;
-
-		sku_info->brand =
-		    fdt_getprop(fdt, wl_tag_node, "brand-code", NULL);
-	} else {
-		/* Not a whitelabel */
-		sku_info->signature_id = fdt_get_name(fdt, model_node, NULL);
-		sku_info->brand = fdt_getprop(fdt, target, "brand-code", NULL);
-		if (!sku_info->brand)
-			sku_info->brand =
-			    fdt_getprop(fdt, model_node, "brand-code", NULL);
-	}
-	sku_info->model = fdt_get_name(fdt, model_node, NULL);
-
-	/* Default customization should be model, or model-wltag. */
-	customization = mosys_strdup(sku_info->signature_id);
-	if (!customization) {
-		lprintf(LOG_ERR, "Could not allocate customization string\n");
-		return -ENOMEM;
-	}
-	sku_info->customization = strupper(customization);
-
-	/* we don't report the sub-model in mosys */
-	lprintf(LOG_DEBUG, "%s: Found model '%s'\n", __func__, sku_info->model);
-
-	return 0;
-err:
-	lprintf(LOG_ERR, "%s: Could not locate SKU in mapping\n", __func__);
-	return -1;
-}
 
 #ifdef CONFIG_PLATFORM_ARCH_X86
 /** internal function with common code to read sku info */
@@ -373,35 +80,11 @@
 				       const char *smbios_name,
 				       struct sku_info *sku_info)
 {
-	const char *platform_name;
-	extern char __dtb_config_begin[];
-	char *fdt = __dtb_config_begin;
-	int ret;
-	lprintf(LOG_DEBUG, "%s: Yaml lookup SMBIOS name '%s', SKU ID %d\n",
+	lprintf(LOG_DEBUG, "%s: JSON lookup SMBIOS name '%s', SKU ID %d\n",
 		__func__, smbios_name ? smbios_name : "(null)", sku_number);
 
-	ret = cros_config_read_sku_info_struct(intf, smbios_name, sku_number,
+	return cros_config_read_sku_info_struct(intf, smbios_name, sku_number,
 					       sku_info);
-	if (!ret)
-		return 0;
-
-	/* Fall back to using device tree if yaml-based config is not present */
-	lprintf(LOG_DEBUG,
-		"%s: Yaml lookup failed, trying device tree: "
-		"SMBIOS name '%s', SKU ID %d\n",
-		__func__, smbios_name ? smbios_name : "(null)", sku_number);
-	ret = cros_config_setup_sku(fdt, sku_info, smbios_name, sku_number,
-				    NULL, &platform_name);
-	if (ret) {
-		if (ret != -ENOENT)
-			lprintf(LOG_ERR,
-				"%s: Failed to read master configuration\n",
-				__func__);
-		return -1;
-	}
-	intf->name = platform_name;
-
-	return 0;
 }
 
 #endif // CONFIG_PLATFORM_ARCH_X86
diff --git a/lib/cros_config/meson.build b/lib/cros_config/meson.build
index 11bce33..14d3c06 100644
--- a/lib/cros_config/meson.build
+++ b/lib/cros_config/meson.build
@@ -6,6 +6,3 @@
   )
 endif
 
-cros_config_dtb_src = files(
-  'config.dtb.S',
-)
diff --git a/meson.build b/meson.build
index 188a8de..d31c298 100644
--- a/meson.build
+++ b/meson.build
@@ -59,28 +59,12 @@
   fmap_dep,
   uuid_dep,
 ]
-link_whole = []
-objects = []
 
 # Cros config is a special snowflake.
 if use_cros_config
   libmosys_src += mosys_lib_cros_config_src
   fdt_dep = meson.get_compiler('c').find_library('fdt')
   deps += fdt_dep
-  dtb_lib = static_library(
-    'cros_config_dtb',
-    cros_config_dtb_src,
-    c_args: [
-      '-D__ASSEMBLY__',
-      '-c',
-    ],
-    link_args: [
-      '-znoexecstack',
-      '-r',
-    ],
-  )
-  link_whole += dtb_lib
-  objects = dtb_lib.extract_all_objects()
 endif
 
 # Lib momsys shared library target
@@ -89,7 +73,5 @@
   libmosys_src,
   dependencies: deps,
   include_directories: include_common,
-  link_whole: link_whole,
-  objects: objects,
   pic: true,
 )