From patchwork Mon Nov 6 11:14:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 741337 Delivered-To: patch@linaro.org Received: by 2002:adf:fd90:0:b0:32d:baff:b0ca with SMTP id d16csp1026781wrr; Mon, 6 Nov 2023 03:16:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IFmTbqp0d040tehjgTS7/6f/BM0tlejfe1at11X+iTjiAIfUQCy7aFq1HXvopa8xBzRAelH X-Received: by 2002:a17:906:794d:b0:9be:705:d7d0 with SMTP id l13-20020a170906794d00b009be0705d7d0mr12528367ejo.0.1699269417501; Mon, 06 Nov 2023 03:16:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699269417; cv=none; d=google.com; s=arc-20160816; b=MnleEhTgLextfC5wL2x5iwKODqxrT0O4OyzlzaueA4kpwOjGRQ05Vmx6IAGxdbPYXF OTHhOvwvdxMFEd53462fguub+KnrgDYjmSFMyzDRA8GepyQ0IUeJwRLJEy0xT1iazZWZ arrwv5NefWM0p8vKQ/3qnqAcUwWy18xiuq6flZpU+j4Ya0l8IdHRjF1UtiF9j8zosuOB syH/xz04qLk57wMuFFtc1kAQ3IYfwqhn3EUQhbfD5mntzKCu4BPspbbLBoy/4ZR3Jg9R ukCmACKVNSrdluYHEu/tUyQOcTPkuk6jPrftwZ88UzMfFAimF5opjWmZuLlhpw9Yg/hA Y53w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=AqNZNjqQYhADk2f9LcxrecaAQ0zvQd04y6QhLiDYaOs=; fh=eBItVz4B+jX5FuRNHVjUaldxTsf8xOQKLrDTtjpPbrk=; b=rQDMNQ1jZZ6tFPltarfkz/2HIruR2mVBL3kfLv7fj6kqd2Prei5KI8V8MGoERp77Eu A82h73OgTbEoRu9UDLQcNhpT1yn0O/L1EqRA/ZCFE45bWTU9V0pMnqGANDl34E0IPjJx 3S4l6NB2XSqg8bzsRmbii33qTVfozRzX7shUKbUe48dNLkEFDJr0h48zHxhSvdCEMFPM siRCIcWiXp0q+8pncflN9lqfa4VFoLflzxvPxxAzGoH0nweBn8MiKZX7jxVUgWzhagUE 0puNLsZkAhK64uvOAWwBKuWE86vm0S/85M28yk9dzc3wFbfCk212AXvx6ZxEEtOI0ipB 7yeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=D7ljuDqm; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id vg18-20020a170907d31200b009ad833b57e1si4514472ejc.195.2023.11.06.03.16.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 03:16:57 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=D7ljuDqm; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 7892F86F91; Mon, 6 Nov 2023 12:16:45 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="D7ljuDqm"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1E62C86C33; Mon, 6 Nov 2023 12:16:44 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 8348686F42 for ; Mon, 6 Nov 2023 12:16:40 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=masahisa.kojima@linaro.org Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-6c311ca94b4so3645531b3a.3 for ; Mon, 06 Nov 2023 03:16:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1699269398; x=1699874198; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=AqNZNjqQYhADk2f9LcxrecaAQ0zvQd04y6QhLiDYaOs=; b=D7ljuDqmFDJF+PQT4hgzAUbpfhvnDnX53tiJqnuidozxZaC3oclXBltv5a/9cxCPeW fkt8/SBESMWCUTDHmOyfxiO93FO6drAu6wG41aZSLGeL5vmuGdM8DV6vsugcVP8DWacC vZ2T0/13Gm98BlQMwbu6oC8Rab8nA5I+dZ/pwaYoq++liaXJe43DSYhYVymaFwrmZ8uQ 1tu2ZfobJ7xv1nxdWvlqIQmXiOWp6w3bJYndMJ0GlC0f0dPVZ9I6mWLPDCuDxbLv5mhR ACzr/Z7AwWIrGXUMq5msmjH4NwzmJVWcpMev09gY4NIkx7ZdLGLeyzvZ8J12YO262RWf LpxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699269398; x=1699874198; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AqNZNjqQYhADk2f9LcxrecaAQ0zvQd04y6QhLiDYaOs=; b=QqNjC4s/YltwLUjlSuux5uAx1T3Qdon+J+jn8bq8i0Ie2PwI/ITLWy+qVmic4u6N/R QnHOiyI77hedO1p3SliBN3BCIelfVhSoBfcZ2g0OssCfmi1JQ+uAz7wi56C2zuMzaSDJ xx6zvCDUPmrp7n8F3SW0RM1WwUpxt8OFxBaC8XEUfrqTYISFI4wpXAxVWgeFMi2LHPYb yalXiBAvU2z12MFF1EyUL+Rsy+beZWFhfBy3ozFPn6iX7qC6o3xDDYe0PSDKGZceceCS AArIIiL4ivDOMVF+pXmI9syZhXEeUrNLACA5ehUhnhgekhIB3T2qIkLTnDADa2tPtPfb HZUA== X-Gm-Message-State: AOJu0YytytpCkjuJiDq1aGCK+HZU7uFx1zlPVcPxzo7HqMrxguPMFDSB Vt4LBn7Cj79q4tsvij1+1QO0xxQL9Dv43xEOhgo= X-Received: by 2002:a05:6a00:1143:b0:693:3851:bd3e with SMTP id b3-20020a056a00114300b006933851bd3emr26639258pfm.3.1699269398675; Mon, 06 Nov 2023 03:16:38 -0800 (PST) Received: from localhost ([164.70.16.189]) by smtp.gmail.com with ESMTPSA id n1-20020aa78a41000000b0068fbaea118esm5467580pfa.45.2023.11.06.03.16.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 03:16:38 -0800 (PST) From: Masahisa Kojima To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Simon Glass , Takahiro Akashi , Michal Simek , Masahisa Kojima , Ramon Fried , Joe Hershberger Subject: [PATCH v10 1/9] net: wget: prevent overwriting reserved memory Date: Mon, 6 Nov 2023 20:14:56 +0900 Message-Id: <20231106111504.3631058-2-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231106111504.3631058-1-masahisa.kojima@linaro.org> References: <20231106111504.3631058-1-masahisa.kojima@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean This introduces the valid range check to store the received blocks using lmb. The same logic is implemented in tftp. Signed-off-by: Masahisa Kojima Acked-by: Ilias Apalodimas Reviewed-by: Simon Glass Reviewed-by: Ramon Fried --- net/wget.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/net/wget.c b/net/wget.c index 8bb4d72db1..6f97eb1d12 100644 --- a/net/wget.c +++ b/net/wget.c @@ -4,16 +4,20 @@ * Copyright Duncan Hare 2017 */ +#include #include #include #include #include #include +#include #include #include #include #include +DECLARE_GLOBAL_DATA_PTR; + static const char bootfile1[] = "GET "; static const char bootfile3[] = " HTTP/1.0\r\n\r\n"; static const char http_eom[] = "\r\n\r\n"; @@ -56,6 +60,29 @@ static unsigned int retry_tcp_ack_num; /* TCP retry acknowledge number*/ static unsigned int retry_tcp_seq_num; /* TCP retry sequence number */ static int retry_len; /* TCP retry length */ +static ulong wget_load_size; + +/** + * wget_init_max_size() - initialize maximum load size + * + * Return: 0 if success, -1 if fails + */ +static int wget_init_load_size(void) +{ + struct lmb lmb; + phys_size_t max_size; + + lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob); + + max_size = lmb_get_free_size(&lmb, image_load_addr); + if (!max_size) + return -1; + + wget_load_size = max_size; + + return 0; +} + /** * store_block() - store block in memory * @src: source of data @@ -64,10 +91,25 @@ static int retry_len; /* TCP retry length */ */ static inline int store_block(uchar *src, unsigned int offset, unsigned int len) { + ulong store_addr = image_load_addr + offset; ulong newsize = offset + len; uchar *ptr; - ptr = map_sysmem(image_load_addr + offset, len); + if (IS_ENABLED(CONFIG_LMB)) { + ulong end_addr = image_load_addr + wget_load_size; + + if (!end_addr) + end_addr = ULONG_MAX; + + if (store_addr < image_load_addr || + store_addr + len > end_addr) { + printf("\nwget error: "); + printf("trying to overwrite reserved memory...\n"); + return -1; + } + } + + ptr = map_sysmem(store_addr, len); memcpy(ptr, src, len); unmap_sysmem(ptr); @@ -248,25 +290,39 @@ static void wget_connected(uchar *pkt, unsigned int tcp_seq_num, net_boot_file_size = 0; - if (len > hlen) - store_block(pkt + hlen, 0, len - hlen); + if (len > hlen) { + if (store_block(pkt + hlen, 0, len - hlen) != 0) { + wget_loop_state = NETLOOP_FAIL; + wget_fail("wget: store error\n", tcp_seq_num, tcp_ack_num, action); + net_set_state(NETLOOP_FAIL); + return; + } + } debug_cond(DEBUG_WGET, "wget: Connected Pkt %p hlen %x\n", pkt, hlen); for (i = 0; i < pkt_q_idx; i++) { + int err; + ptr1 = map_sysmem( (phys_addr_t)(pkt_q[i].pkt), pkt_q[i].len); - store_block(ptr1, - pkt_q[i].tcp_seq_num - - initial_data_seq_num, - pkt_q[i].len); + err = store_block(ptr1, + pkt_q[i].tcp_seq_num - + initial_data_seq_num, + pkt_q[i].len); unmap_sysmem(ptr1); debug_cond(DEBUG_WGET, "wget: Connctd pkt Q %p len %x\n", pkt_q[i].pkt, pkt_q[i].len); + if (err) { + wget_loop_state = NETLOOP_FAIL; + wget_fail("wget: store error\n", tcp_seq_num, tcp_ack_num, action); + net_set_state(NETLOOP_FAIL); + return; + } } } } @@ -338,6 +394,7 @@ static void wget_handler(uchar *pkt, u16 dport, len) != 0) { wget_fail("wget: store error\n", tcp_seq_num, tcp_ack_num, action); + net_set_state(NETLOOP_FAIL); return; } @@ -428,6 +485,15 @@ void wget_start(void) debug_cond(DEBUG_WGET, "\nwget:Load address: 0x%lx\nLoading: *\b", image_load_addr); + if (IS_ENABLED(CONFIG_LMB)) { + if (wget_init_load_size()) { + printf("\nwget error: "); + printf("trying to overwrite reserved memory...\n"); + net_set_state(NETLOOP_FAIL); + return; + } + } + net_set_timeout_handler(wget_timeout, wget_timeout_handler); tcp_set_tcp_handler(wget_handler); From patchwork Mon Nov 6 11:14:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 741338 Delivered-To: patch@linaro.org Received: by 2002:adf:fd90:0:b0:32d:baff:b0ca with SMTP id d16csp1026849wrr; Mon, 6 Nov 2023 03:17:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IHW0Cs76QeoB0opPT4tso1tbpWLYK8HGdxoZu3ElLXxM1F3lHjuWzP4ykTjwUGJBtAHUfAn X-Received: by 2002:a17:907:6e89:b0:9b9:4509:d575 with SMTP id sh9-20020a1709076e8900b009b94509d575mr15599602ejc.2.1699269431276; Mon, 06 Nov 2023 03:17:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699269431; cv=none; d=google.com; s=arc-20160816; b=UChyZZoYP43qnHwYPHLk0cRcOfjCflN/WMC5ChX6/JW08GGMSgVUM63mVEwet/KwPJ OxPxRsXn6gLkGRIl9eJ/mUDOA0hkmDv0o0v5T12x51CyFwp3KENN76K0+jLQ9GxoIvgN Ixi87MX5W0F4d7DFfT7wxnoL+Dvp6OIqNmCRAku1jPCZXx9E842XBVNIFv9x0CTvSWr1 XagD7AlRpxrXQdKhJhG5lfsiEzwVBlL32cmGCENeEsYtBmHNBxrHhCxmMCu6Y3RjZKUJ seLp1LQEs7axgfIsEMA9l9d6jHpoJE92YGQAcz6lj11eNoQf+33YpKFgxXUVUj36anH4 UzVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=LpkU9wk3gj829EVx8p2lHMNKkbubof1kR3KeYVvX1vo=; fh=eBItVz4B+jX5FuRNHVjUaldxTsf8xOQKLrDTtjpPbrk=; b=sX5U8YYQ+JjbmeKLRKbipF+Ae5wgVz52MMEk2KETuwNFeEeZKnlINW6Do1+PJt1L7q T3l8F9uXQ082O/y2yi8nWMN1p39frDuebu/e3ZAV0FwHDhp8+E4BZBhIWxNFZ1dvr3tH RfL+FY+zbQoJbEQfsqRzhdK0+PsXfCztlZT7jNKPTjZow0s6V6mJizWdkEl7fA5CVdhv IuQAnY6P4ccs+EOFUCdsQJf0PbYpPBX+rsKYEUAJTcu+Yxd3qUtAT0v0ICvrKaBv/bz1 m4hgpLugJfdxtYUP2G7nZJQhBnfwQksCUQ8AXQwR7dCOIalUYUWiZ5faF1sYpz5xjYD9 d2fA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="Ed/YwHgd"; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id hs11-20020a1709073e8b00b009b92b945591si4208870ejc.177.2023.11.06.03.17.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 03:17:11 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="Ed/YwHgd"; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id E371F86C33; Mon, 6 Nov 2023 12:17:09 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="Ed/YwHgd"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 49EE586FBD; Mon, 6 Nov 2023 12:16:47 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-oi1-x22c.google.com (mail-oi1-x22c.google.com [IPv6:2607:f8b0:4864:20::22c]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 154ED86F68 for ; Mon, 6 Nov 2023 12:16:44 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=masahisa.kojima@linaro.org Received: by mail-oi1-x22c.google.com with SMTP id 5614622812f47-3b3e7f56ca4so2383707b6e.0 for ; Mon, 06 Nov 2023 03:16:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1699269402; x=1699874202; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=LpkU9wk3gj829EVx8p2lHMNKkbubof1kR3KeYVvX1vo=; b=Ed/YwHgdQOj18GvQzcDhIJ/B3Eh7/Uos7vfe4bJ1esnwI0buMclQthdWyNs5qQPnGA aHexe4uJYHfJKLY4mX0jc9atFJGeuQGEVjbGYEhGeHbr+UqHyQXniX3n9pOhy2DMPFx8 ONWbdMEiVHxgvNXaKAyd+mHSUGL8x4LUMBcBL5HHxQdcvlnUh+ngWDX9NYGgXHFYEUIV VCqLAszhkORO0a148JBeTwcRJN70pFZcKUki65Pj5rbRM8+e3z6ZtD9eCkTDgbIwlyBX v8SUcM5yEgfvh+SEKs5V9ITiBPKNUb7qD1UTmupkXqH0QIYDzgkG4bgXUpZekBawXv/U THdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699269402; x=1699874202; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LpkU9wk3gj829EVx8p2lHMNKkbubof1kR3KeYVvX1vo=; b=Pr1lZtbkdTjtiAljvquD4Uhk/T/T4Zq21PYa+8CLXWbkYb0mxWcaMcB3L7jq93yMOF DSgAv4PRaOB+4+bGKo1Ri2gi0JxxlIY+bXl5WfbHe348FddLrHTRmLoYkIFLSia8mJ81 q51HMw81ZTvk19dY6w4EMd5cjchxaoq22L49SuOl8IEqLgPBSIzSBqJrSwd+MkzH6h7J obLa3WgrREnOVKmIOjEVGcbGUDkSJ2m9ddvt6O+KzWHEzFXIFt6GyxKiAnciW7XDR/6t MtW2miT9mMHdsw7ZKtpSyN9+mjPBDXfDVy8mVPBcsjqa5Ueb4Z3IfEGa9Of1gLKYMyhz +xdA== X-Gm-Message-State: AOJu0YzILWn4g/sEbPojUppR5RGqJBuMvEH/bx41x4hl9tpJLgCbSLTu sY4IWwR69nDq6ZNMgq1WMbzZvas3EOzpcWtYgyk= X-Received: by 2002:a05:6808:8f6:b0:3ae:511e:179e with SMTP id d22-20020a05680808f600b003ae511e179emr28702985oic.54.1699269402466; Mon, 06 Nov 2023 03:16:42 -0800 (PST) Received: from localhost ([164.70.16.189]) by smtp.gmail.com with ESMTPSA id u4-20020aa78484000000b006bd8f4e398csm5437676pfn.135.2023.11.06.03.16.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 03:16:41 -0800 (PST) From: Masahisa Kojima To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Simon Glass , Takahiro Akashi , Michal Simek , Masahisa Kojima , Ramon Fried , Joe Hershberger Subject: [PATCH v10 2/9] net: wget: add wget with dns utility function Date: Mon, 6 Nov 2023 20:14:57 +0900 Message-Id: <20231106111504.3631058-3-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231106111504.3631058-1-masahisa.kojima@linaro.org> References: <20231106111504.3631058-1-masahisa.kojima@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Current wget takes the target uri in this format: ":" e.g.) 192.168.1.1:/bar The http server ip address must be resolved before calling wget. This commit adds the utility function runs wget with dhs. User can call wget with the uri like "http://foo/bar". Signed-off-by: Masahisa Kojima Reviewed-by: Ilias Apalodimas Reviewed-by: Ramon Fried --- include/net.h | 9 +++++++++ net/wget.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/include/net.h b/include/net.h index e254df7d7f..57889d8b7a 100644 --- a/include/net.h +++ b/include/net.h @@ -926,4 +926,13 @@ void eth_set_enable_bootdevs(bool enable); static inline void eth_set_enable_bootdevs(bool enable) {} #endif +/** + * wget_with_dns() - runs dns host IP address resulution before wget + * + * @dst_addr: destination address to download the file + * @uri: uri string of target file of wget + * Return: downloaded file size, negative if failed + */ +int wget_with_dns(ulong dst_addr, char *uri); + #endif /* __NET_H__ */ diff --git a/net/wget.c b/net/wget.c index 6f97eb1d12..2087146b37 100644 --- a/net/wget.c +++ b/net/wget.c @@ -15,6 +15,7 @@ #include #include #include +#include DECLARE_GLOBAL_DATA_PTR; @@ -512,3 +513,56 @@ void wget_start(void) wget_send(TCP_SYN, 0, 0, 0); } + +#if (IS_ENABLED(CONFIG_CMD_DNS)) +int wget_with_dns(ulong dst_addr, char *uri) +{ + int ret; + char *s, *host_name, *file_name, *str_copy; + + /* + * Download file using wget. + * + * U-Boot wget takes the target uri in this format. + * ":" e.g.) 192.168.1.1:/sample/test.iso + * Need to resolve the http server ip address before starting wget. + */ + str_copy = strdup(uri); + if (!str_copy) + return -ENOMEM; + + s = str_copy + strlen("http://"); + host_name = strsep(&s, "/"); + if (!s) { + log_err("Error: invalied uri, no file path\n"); + ret = -EINVAL; + goto out; + } + file_name = s; + + /* TODO: If the given uri has ip address for the http server, skip dns */ + net_dns_resolve = host_name; + net_dns_env_var = "httpserverip"; + if (net_loop(DNS) < 0) { + log_err("Error: dns lookup of %s failed, check setup\n", net_dns_resolve); + ret = -EINVAL; + goto out; + } + s = env_get("httpserverip"); + if (!s) { + ret = -EINVAL; + goto out; + } + + strlcpy(net_boot_file_name, s, sizeof(net_boot_file_name)); + strlcat(net_boot_file_name, ":/", sizeof(net_boot_file_name)); /* append '/' which is removed by strsep() */ + strlcat(net_boot_file_name, file_name, sizeof(net_boot_file_name)); + image_load_addr = dst_addr; + ret = net_loop(WGET); + +out: + free(str_copy); + + return ret; +} +#endif From patchwork Mon Nov 6 11:14:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 741340 Delivered-To: patch@linaro.org Received: by 2002:adf:fd90:0:b0:32d:baff:b0ca with SMTP id d16csp1027012wrr; Mon, 6 Nov 2023 03:17:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IEYV0lIBVWxosPZZCISVVKiwULjYI0jHzQ+ke46N+fNL8csdXLoRA7l4Yr+7TByzsnnFnfC X-Received: by 2002:a17:906:db0e:b0:9d3:8d1e:cee with SMTP id xj14-20020a170906db0e00b009d38d1e0ceemr14938157ejb.70.1699269455181; Mon, 06 Nov 2023 03:17:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699269455; cv=none; d=google.com; s=arc-20160816; b=SI8jH0rzZfNrZ3z/YaVkvZkdn2nHJzUk42xjxAYX/pX+jxJyyKbnSbeumpwyaMUttI ShGQ/SJHpqrApvXW8Vzf3wWMvgMN/fd1VwyItF16ekHf6S0GVRm2MLysHZ1N8pQbP6De NSRysmCg0g3DtTUE49IPOrpNy6CySRJ67Ou1EaYjF5vb815pDU2qe7hDMdRB5VOiXo4o wD4oNZt6MJ6lwQlIqSnM2cTDIk/aIvjoCG97qwkpAjB5fISsyASGaFgcJJBgt6lvO7Kk n+eEt8AR1FiT+5yJUq+issyuRKlMguRhBEEgJqtu6KmlWiNOf8ag6FdRfrFDszExWp6a 641A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=IW6UuW0tl+AFM9nTW8wKwyaIsXdRioIs6zkSPzK/8Ik=; fh=zqO2vTczeIKx1OTsr8uErpGVOeopTGT8suxWOXUGpbQ=; b=b03aTBh8kfTDXNBT/38JBmRvNZATMxYIfFWaqliwgbR9flEo5uq1j351leO7uo/8Zx 1a4dcZ6xM/KB7TnU033d1ciWvU9iumBsPBOtLcgtigaTNcX3eH7F26U1wb4EsSiWxtVP 1kOqe9T3R2T6/GuDRu2iq5HtTlxY9cIIcCsFtWtXwQT4ax5V4Ki+JzSk5G+0+bTxLem6 1Xa9ec5f9/DaDztu5Gh4XZ706dqt1mrxQFYvLU+E36RjzHutoYpw9fKPWBZeXG2DzO0Q kitpPfvN/nlmE4GQS/cq6jYd3JxucWjG421T1PHUPXW1st74FBouKEUJo6GMI6/WLH3A HqPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=NSzOEAkk; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id di12-20020a170906730c00b009c7523e2b89si4109357ejc.12.2023.11.06.03.17.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 03:17:35 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=NSzOEAkk; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id A210E86BEC; Mon, 6 Nov 2023 12:17:34 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="NSzOEAkk"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C3EBD86FF2; Mon, 6 Nov 2023 12:17:04 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-oi1-x22b.google.com (mail-oi1-x22b.google.com [IPv6:2607:f8b0:4864:20::22b]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 773B086FBD for ; Mon, 6 Nov 2023 12:16:48 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=masahisa.kojima@linaro.org Received: by mail-oi1-x22b.google.com with SMTP id 5614622812f47-3b2f5aed39cso2607546b6e.1 for ; Mon, 06 Nov 2023 03:16:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1699269406; x=1699874206; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=IW6UuW0tl+AFM9nTW8wKwyaIsXdRioIs6zkSPzK/8Ik=; b=NSzOEAkkQzT9SOEZRgtikVWbTPG5Ehz8is/fNPL5+Gxz1zdbWzEJBft7Mns3SWjzxK A3GdWhsEWQWjAF0C51JELFPI2ksPEr8QMIia+RSPX8DRA5dpqk1vK9byYk6jcMK2oah1 vHUpCMU10nqnvzjl/S10rgHfE1u6d90VWKhCpMzOZud7lRML5RWYhNruO25aOkq0UONd f2gOZ9X2YPYD2YlUgSyLzob/tKT7Y84y4dCruDhaw4STy9XKkeTxshIo57099wc0JnZ2 IYoyqDeCjab9bajtZWLNPoie9OeZDbnyzuv0kkiJvig65Wv31blPSpPBnTC1hKi+GVH7 V7AA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699269407; x=1699874207; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IW6UuW0tl+AFM9nTW8wKwyaIsXdRioIs6zkSPzK/8Ik=; b=ABHl7e0jIJfS+73KSWkOY1EYC8076wvKgdQZFK+qutoaRXEU5BItDx44wdEA7CuD+W KYJCxlH4vzo9+t8rC3jHzeI7sskEddj5cRnRjlQem0Jkljdci1hW015NrVZO3f9h1OSn Lm4mKlycXkoCeGk4hdlyr5BDRaEccPgiR2yOqmHqb4dLc/1Fo2cDPOR8vVSWuoOud6YB Yy17Ax8aBW3jHdfKeuj4EhQcbyttn9VwZOZoPaaWFB/+iWkiZpO3E19q/B/d4Q3oecfE Hv4+ZunxHoeLO9kLuOJGpNboL835fk3Sq3+qsu0yJpIbUu36clpEacxjAOYSGTiVf/Cd L/IA== X-Gm-Message-State: AOJu0YxZu/c2FioB6nlj3k5l9Pr8WjbOJAlGxHxdWU7ma+Etl6jEUKoQ WPjf1FurO1lkU+SMg/Vz9WJTdG0B8GET1UO4rZs= X-Received: by 2002:a05:6358:3399:b0:16b:66d3:70af with SMTP id i25-20020a056358339900b0016b66d370afmr3938220rwd.7.1699269406698; Mon, 06 Nov 2023 03:16:46 -0800 (PST) Received: from localhost ([164.70.16.189]) by smtp.gmail.com with ESMTPSA id e186-20020a6369c3000000b0057ab7d42a4dsm5331042pgc.86.2023.11.06.03.16.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 03:16:46 -0800 (PST) From: Masahisa Kojima To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Simon Glass , Takahiro Akashi , Michal Simek , Masahisa Kojima , Tobias Waldekranz Subject: [PATCH v10 3/9] blk: blkmap: add ramdisk creation utility function Date: Mon, 6 Nov 2023 20:14:58 +0900 Message-Id: <20231106111504.3631058-4-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231106111504.3631058-1-masahisa.kojima@linaro.org> References: <20231106111504.3631058-1-masahisa.kojima@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean User needs to call several functions to create the ramdisk with blkmap. This adds the utility function to create blkmap device and mount the ramdisk. Signed-off-by: Masahisa Kojima Reviewed-by: Simon Glass Reviewed-by: Ilias Apalodimas --- drivers/block/Makefile | 3 +- drivers/block/blkmap.c | 15 ---------- drivers/block/blkmap_helper.c | 53 +++++++++++++++++++++++++++++++++++ include/blkmap.h | 29 +++++++++++++++++++ 4 files changed, 84 insertions(+), 16 deletions(-) create mode 100644 drivers/block/blkmap_helper.c diff --git a/drivers/block/Makefile b/drivers/block/Makefile index a161d145fd..ec0575d135 100644 --- a/drivers/block/Makefile +++ b/drivers/block/Makefile @@ -14,7 +14,8 @@ obj-$(CONFIG_IDE) += ide.o endif obj-$(CONFIG_SANDBOX) += sandbox.o host-uclass.o host_dev.o obj-$(CONFIG_$(SPL_TPL_)BLOCK_CACHE) += blkcache.o -obj-$(CONFIG_BLKMAP) += blkmap.o +obj-$(CONFIG_$(SPL_TPL_)BLKMAP) += blkmap.o +obj-$(CONFIG_$(SPL_TPL_)BLKMAP) += blkmap_helper.o obj-$(CONFIG_EFI_MEDIA) += efi-media-uclass.o obj-$(CONFIG_EFI_MEDIA_SANDBOX) += sb_efi_media.o diff --git a/drivers/block/blkmap.c b/drivers/block/blkmap.c index 149a4cac3e..21201409ed 100644 --- a/drivers/block/blkmap.c +++ b/drivers/block/blkmap.c @@ -66,21 +66,6 @@ struct blkmap_slice { void (*destroy)(struct blkmap *bm, struct blkmap_slice *bms); }; -/** - * struct blkmap - Block map - * - * Data associated with a blkmap. - * - * @label: Human readable name of this blkmap - * @blk: Underlying block device - * @slices: List of slices associated with this blkmap - */ -struct blkmap { - char *label; - struct udevice *blk; - struct list_head slices; -}; - static bool blkmap_slice_contains(struct blkmap_slice *bms, lbaint_t blknr) { return (blknr >= bms->blknr) && (blknr < (bms->blknr + bms->blkcnt)); diff --git a/drivers/block/blkmap_helper.c b/drivers/block/blkmap_helper.c new file mode 100644 index 0000000000..bfba14110d --- /dev/null +++ b/drivers/block/blkmap_helper.c @@ -0,0 +1,53 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * blkmap helper function + * + * Copyright (c) 2023, Linaro Limited + */ + +#include +#include +#include +#include + +int blkmap_create_ramdisk(const char *label, ulong image_addr, ulong image_size, + struct udevice **devp) +{ + int ret; + lbaint_t blknum; + struct blkmap *bm; + struct blk_desc *desc; + struct udevice *bm_dev; + + ret = blkmap_create(label, &bm_dev); + if (ret) { + log_err("failed to create blkmap\n"); + return ret; + } + + bm = dev_get_plat(bm_dev); + desc = dev_get_uclass_plat(bm->blk); + blknum = image_size >> desc->log2blksz; + ret = blkmap_map_pmem(bm_dev, 0, blknum, image_addr); + if (ret) { + log_err("Unable to map %#llx at block %d : %d\n", + (unsigned long long)image_addr, 0, ret); + goto err; + } + log_info("Block %d+0x" LBAF " mapped to %#llx\n", 0, blknum, + (unsigned long long)image_addr); + + ret = device_probe(bm->blk); + if (ret) + goto err; + + if (devp) + *devp = bm_dev; + + return 0; + +err: + blkmap_destroy(bm_dev); + + return ret; +} diff --git a/include/blkmap.h b/include/blkmap.h index af54583c7d..30dc84a7da 100644 --- a/include/blkmap.h +++ b/include/blkmap.h @@ -7,6 +7,23 @@ #ifndef _BLKMAP_H #define _BLKMAP_H +#include + +/** + * struct blkmap - Block map + * + * Data associated with a blkmap. + * + * @label: Human readable name of this blkmap + * @blk: Underlying block device + * @slices: List of slices associated with this blkmap + */ +struct blkmap { + char *label; + struct udevice *blk; + struct list_head slices; +}; + /** * blkmap_map_linear() - Map region of other block device * @@ -74,4 +91,16 @@ int blkmap_create(const char *label, struct udevice **devp); */ int blkmap_destroy(struct udevice *dev); +/** + * blkmap_create_ramdisk() - Create new ramdisk with blkmap + * + * @label: Label of the new blkmap + * @image_addr: Target memory start address of this mapping + * @image_size: Target memory size of this mapping + * @devp: Updated with the address of the created blkmap device + * Returns: 0 on success, negative error code on failure + */ +int blkmap_create_ramdisk(const char *label, ulong image_addr, ulong image_size, + struct udevice **devp); + #endif /* _BLKMAP_H */