From patchwork Tue May 21 18:37:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elliot Berman X-Patchwork-Id: 13669641 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B0FE6C25B7C for ; Tue, 21 May 2024 18:39:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:CC:To:In-Reply-To:References:Message-ID :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=gdkEcoatIOjVc+WszuMe/dYyZmQr027IxKBV6MMZCCk=; b=N/c30h3Xp2kkS6 w6fHrVb350bPRlgiQ1uakb0ZzRrP00eceAv23l5KksCCiM99Jrpaov22GYqcW/KX+qnVlHuhuqzfN 0I3Ed1IHfs267f7aTFTouXaeaTdT+IROWsTfrSuFdVc/izO9xYhFWwYHMlGa1Z4EWbzntTMN69u4t 6rmh8EeY2Dz52Lv18YCwst8B080cSOTf4PudeFGIAtXrd3+gYEZj66fdDJgMVWsQs3C9dtPCnKSSH dSy549PUOd8B4FhNfrhNCgL2K/gI30PqJQeTqh07BdchqtbEVmUtAwWOXt6e2EL07RlAqk5bcrNAa vPl4aBF9bqChII39WbHA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1s9UNy-00000000nYX-42lK; Tue, 21 May 2024 18:38:54 +0000 Received: from mx0a-0031df01.pphosted.com ([205.220.168.131]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1s9UNk-00000000nS9-0ZHL for linux-arm-kernel@lists.infradead.org; Tue, 21 May 2024 18:38:43 +0000 Received: from pps.filterd (m0279866.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 44LAA2GQ017585; Tue, 21 May 2024 18:38:27 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= from:date:subject:mime-version:content-type :content-transfer-encoding:message-id:references:in-reply-to:to :cc; s=qcppdkim1; bh=wFapvs5sqECt2yxAZvhfcK0g+PRdjhRiLtTWYhQWucQ =; b=NAGyhtbMm5LAUGeze8VjPInLHswAvQtfgjjxfinPp7Jabyk95u7T2QAbTVj wId82CdOgOstbO2oesqg6JAoQbyhi62Ra6kebTfHLzXSFAomcl2xNDhE96GOQAE5 2nmamIZnNfKJhydqr/QMn0ERgbKWg8t16sLeY/x/N/zfSZt9wBiDeKSZP0wjjsOC EIl0QdsfZhYJ5d0By9bhSJu4njx/uQMePlcmNKMbGVjIW/ZDfUjr4obmlIdAv37s dwUUOqlzUhrK2q5LK1basyrsiYkUU4ScSnAKRnLvbAGtgXYy2JsjkEaWK2dSeU22 RWUPU81K6A+QU5BqNpiBAvpwPQQ== Received: from nasanppmta05.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3y6pq5esgw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 21 May 2024 18:38:27 +0000 (GMT) Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA05.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 44LIcP5U012112 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 21 May 2024 18:38:25 GMT Received: from hu-eberman-lv.qualcomm.com (10.49.16.6) by nasanex01b.na.qualcomm.com (10.46.141.250) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Tue, 21 May 2024 11:38:24 -0700 From: Elliot Berman Date: Tue, 21 May 2024 11:37:58 -0700 Subject: [PATCH RFC v3 1/9] libfdt: board-id: Implement board-id scoring MIME-Version: 1.0 Message-ID: <20240521-board-ids-v3-1-e6c71d05f4d2@quicinc.com> References: <20240521-board-ids-v3-0-e6c71d05f4d2@quicinc.com> In-Reply-To: <20240521-board-ids-v3-0-e6c71d05f4d2@quicinc.com> To: Rob Herring , Frank Rowand , Krzysztof Kozlowski , Conor Dooley , Bjorn Andersson , Konrad Dybcio CC: Amrit Anand , Peter Griffin , Caleb Connolly , "Andy Gross" , Konrad Dybcio , "Doug Anderson" , Simon Glass , "Chen-Yu Tsai" , Julius Werner , "Humphreys, Jonathan" , Sumit Garg , "Jon Hunter" , Michal Simek , , , , , , Elliot Berman X-Mailer: b4 0.13.0 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01a.na.qualcomm.com (10.47.209.196) To nasanex01b.na.qualcomm.com (10.46.141.250) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-GUID: 97M0_qhDY4fDkHKWdgZ-TnVPL5Q5oHL6 X-Proofpoint-ORIG-GUID: 97M0_qhDY4fDkHKWdgZ-TnVPL5Q5oHL6 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.650,FMLib:17.12.28.16 definitions=2024-05-21_11,2024-05-21_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 malwarescore=0 adultscore=0 lowpriorityscore=0 priorityscore=1501 suspectscore=0 spamscore=0 clxscore=1015 mlxlogscore=999 mlxscore=0 bulkscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2405010000 definitions=main-2405210140 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240521_113840_256184_3760C727 X-CRM114-Status: GOOD ( 22.26 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The devicetree spec introduced a mechanism to match devicetree blobs to boards using firmware-provided identifiers. Although the matching can be implemented by DTB loaders, having a canonical implementation makes it easier to integrate and ensure consistent behavior across ecosystems. I've not yet investigated swig/python support for the new functions; I would work on that before submitting the patch to libfdt. Signed-off-by: Elliot Berman --- scripts/dtc/libfdt/fdt_ro.c | 76 +++++++++++++++++++++++++++++++++++++++++++++ scripts/dtc/libfdt/libfdt.h | 54 ++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/scripts/dtc/libfdt/fdt_ro.c b/scripts/dtc/libfdt/fdt_ro.c index 9f6c551a22c2..b19b17127399 100644 --- a/scripts/dtc/libfdt/fdt_ro.c +++ b/scripts/dtc/libfdt/fdt_ro.c @@ -857,3 +857,79 @@ int fdt_node_offset_by_compatible(const void *fdt, int startoffset, return offset; /* error from fdt_next_node() */ } + +int fdt_board_id_prop_matches(const void *fdt, + const struct fdt_property *prop, + fdt_get_board_id_t get_board_id_cb, + void *ctx) +{ + int data_len; + const void *data; + const char *name; + int name_len; + int string = 0; + + name = fdt_get_string(fdt, fdt32_to_cpu(prop->nameoff), &name_len); + if (!name) + return -FDT_ERR_BADOFFSET; + + if (name_len > 4 && !strcmp(name + name_len - 4, "_str")) + string = 1; + + data = get_board_id_cb(ctx, name, &data_len); + if (!data) + return -FDT_ERR_NOTFOUND; + + if (string) { + return fdt_stringlist_contains(prop->data, + fdt32_to_cpu(prop->len), + data); + } else { + // exact data comparison. data_len is the size of each entry + if (fdt32_to_cpu(prop->len) % data_len || data_len % 4) + return -FDT_ERR_BADVALUE; + + for (int i = 0; i < fdt32_to_cpu(prop->len); i += data_len) { + if (!memcmp(&prop->data[i], data, data_len)) + return 1; + } + + return 0; + } + + return 0; +} + +int fdt_board_id_score(const void *fdt, fdt_get_board_id_t get_board_id_cb, + void *ctx) +{ + const struct fdt_property *prop; + int node, property, ret, score = 0; + const char *name; + + node = fdt_path_offset(fdt, "/board-id"); + if (node < 0) + return node; + + fdt_for_each_property_offset(property, fdt, node) { + prop = fdt_get_property_by_offset(fdt, property, NULL); + if (!prop) + return -FDT_ERR_BADOFFSET; + + name = fdt_get_string(fdt, fdt32_to_cpu(prop->nameoff), NULL); + if (!name) + return -FDT_ERR_BADOFFSET; + + if (!strcmp(name, "phandle") || !strcmp(name, "linux,phandle")) + continue; + + ret = fdt_board_id_prop_matches(fdt, prop, get_board_id_cb, + ctx); + if (ret == 1) + score++; + else + return ret; + } + + return score; +} diff --git a/scripts/dtc/libfdt/libfdt.h b/scripts/dtc/libfdt/libfdt.h index 77ccff19911e..de1cbc339315 100644 --- a/scripts/dtc/libfdt/libfdt.h +++ b/scripts/dtc/libfdt/libfdt.h @@ -1230,6 +1230,60 @@ int fdt_address_cells(const void *fdt, int nodeoffset); */ int fdt_size_cells(const void *fdt, int nodeoffset); +/**********************************************************************/ +/* Read-only functions (board-id related) */ +/**********************************************************************/ + +/** + * fdt_get_board_id_t - callback to retrieve the board id for the platform + * @ctx: Context data passed from fdt_board_id_prop_matches() + * @name: board id name + * @data: Callback sets pointer to the board id data + * @datalen: Callback sets length of the board id data + * + * returns: + * Pointer to the board id data, NULL if the data doesn't exist + */ +typedef const void *(*fdt_get_board_id_t)(void *ctx, const char *name, + int *datalen); + +/** + * fdt_board_id_prop_matches - check if the property matches the platform's board id + * @fdt: pointer to the device tree blob + * @prop_offset: Offset of the property to match + * @get_board_id_cb: Function pointer to retrieve the platform's board id + * + * returns: + * 1: the property matches + * 0: the property doesn't match + * -FDT_ERR_BADMAGIC, + * -FDT_ERR_BADVERSION, + * -FDT_ERR_BADSTATE, + * -FDT_ERR_BADSTRUCTURE, + * -FDT_ERR_TRUNCATED, standard meanings + */ +int fdt_board_id_prop_matches(const void *fdt, + const struct fdt_property *prop, + fdt_get_board_id_t get_board_id_cb, + void *ctx); + +/** + * fdt_board_id_score - calculate score of the fdt's board_id + * @fdt: pointer to the device tree blob + * @get_board_id_cb: Function pointer to retrieve the platform's board id + * + * returns: + * >0 for the number of board-id properties that were matched + * 0 if there was a mismatch in any of the board-id properties + * -FDT_ERR_BADMAGIC, + * -FDT_ERR_BADVERSION, + * -FDT_ERR_BADSTATE, + * -FDT_ERR_BADSTRUCTURE, + * -FDT_ERR_TRUNCATED, standard meanings + */ +int fdt_board_id_score(const void *fdt, fdt_get_board_id_t get_board_id_cb, + void *ctx); + /**********************************************************************/ /* Write-in-place functions */