From patchwork Sat Sep 21 03:43:53 2024
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
X-Patchwork-Submitter: Mikhail Kshevetskiy
X-Patchwork-Id: 1988113
Return-Path:
X-Original-To: incoming@patchwork.ozlabs.org
Delivered-To: patchwork-incoming@legolas.ozlabs.org
Authentication-Results: legolas.ozlabs.org;
dkim=pass (2048-bit key;
unprotected) header.d=iopsys.eu header.i=@iopsys.eu header.a=rsa-sha256
header.s=selector2 header.b=PUuWNuCC;
dkim-atps=neutral
Authentication-Results: legolas.ozlabs.org;
spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de
(client-ip=85.214.62.61; helo=phobos.denx.de;
envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org)
Received: from phobos.denx.de (phobos.denx.de [85.214.62.61])
(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
key-exchange X25519 server-signature ECDSA (secp384r1))
(No client certificate requested)
by legolas.ozlabs.org (Postfix) with ESMTPS id 4X9Zts60Sxz1y2P
for ; Sat, 21 Sep 2024 13:46:45 +1000 (AEST)
Received: from h2850616.stratoserver.net (localhost [IPv6:::1])
by phobos.denx.de (Postfix) with ESMTP id B16E188BBC;
Sat, 21 Sep 2024 05:44:29 +0200 (CEST)
Authentication-Results: phobos.denx.de;
dmarc=pass (p=reject dis=none) header.from=iopsys.eu
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=iopsys.eu header.i=@iopsys.eu header.b="PUuWNuCC";
dkim-atps=neutral
Received: by phobos.denx.de (Postfix, from userid 109)
id C5577887D1; Sat, 21 Sep 2024 05:44:26 +0200 (CEST)
X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de
X-Spam-Level:
X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,
DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS,
SPF_PASS autolearn=no autolearn_force=no version=3.4.2
Received: from EUR05-DB8-obe.outbound.protection.outlook.com
(mail-db8eur05on20701.outbound.protection.outlook.com
[IPv6:2a01:111:f403:2614::701])
(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
(No client certificate requested)
by phobos.denx.de (Postfix) with ESMTPS id 43AB388641
for ; Sat, 21 Sep 2024 05:44:23 +0200 (CEST)
Authentication-Results: phobos.denx.de;
dmarc=pass (p=reject dis=none) header.from=iopsys.eu
Authentication-Results: phobos.denx.de;
spf=pass smtp.mailfrom=mikhail.kshevetskiy@genexis.eu
ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none;
b=FGcXgHHoPF1sOYFAEHJTtt7N5BCT75UFh5O4w7A/Kdpo7dYDCUZe0S0hMPmlSlCHlxzxWPFsY86ABnhk+REyyxow/84uMNwdCbpKEkc85qUrwjteUX2LKB2kJkdTtYSAz6mQUUDAF/FDoQ8ubdFivqCiNNQZaZzSfa3Y9Cn//cszpA1MH5VnqO8JSo8GB3qvJP/yOuvg1QfQwemyfE89iYlwSvuzD3rdbANInoIVPWPhuMYdWOvQdNBa0/S/GNAhVj+WpFcy+tr7vwnakJFyoxBKyGe6gpb9eG4WXaYm4U8nVb04DsPatZxdjkQJG4O5jxC+vwZOacYhqfNHGf6Yhw==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com;
s=arcselector10001;
h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1;
bh=yAai+B3NX7mh8fnORRGFZEmiBJfbH9GLQrs9gP+sQhs=;
b=gO6o8XgiEKoLdQmE1FC8GgEjhkkLtUfrfLMsO4cZffmOf9v8+WzNEtzm7FbKrL1og9/Z32KEpA5jLnsAXAAoRMxFNUc+lk2gjElsen+CBgGmhnkZJWNKywP6RUqVCOeT7OqgtBF1u/sSzkgnHEjACeeqjcVfuWPr8VgTvZcFZTCd0F2wYkCikqH/+k6gpfhGJUDh+wU0/NrFyqjPMbhsIgLjnjdpVS0Fsh/xQ2O/Z8mg4Nvzq0P0QdckgHsgSZQmsle+H2HXky+Rwpmjb8WOWz5oU4by7/fEsw45gtqiKAOaawfikEk8dwG6M0e9M6N54HUVavZKKvjc8qEjv3u31w==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass
smtp.mailfrom=genexis.eu; dmarc=pass action=none header.from=iopsys.eu;
dkim=pass header.d=iopsys.eu; arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iopsys.eu;
s=selector2;
h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
bh=yAai+B3NX7mh8fnORRGFZEmiBJfbH9GLQrs9gP+sQhs=;
b=PUuWNuCCMJ+zRdP3upuO3G0YygeFuqVeMScNE7GlHqb3Gg7ASzSsRdQGITFfnvq/xaoeTuHZtdeHMSb6r53CsA/QO+ZNdLjo6dm/qIJalrktPxGW2gaOig1YHz/bLweNSJ/+p15pNmq5opMgwWC5GQvYVoUSJPBIWv0uNhTMRQ1+ZPy1CN4c1NCJX/DWqZogpsUaKzSCNhDn5H+DMTkKaU3gvTZthfQKKZ+KefMZ79loUxE6L/wHqH5Tx+52HussjYsrJhM5efASs6savyUlkG5dsoyC7dwEf1JDcYQoNpjVSlq5k7AxUS1i3+xA+yGnC+Z7ctnDWUCqNyEVM3VnCw==
Authentication-Results: dkim=none (message not signed)
header.d=none;dmarc=none action=none header.from=iopsys.eu;
Received: from GV2PR08MB8121.eurprd08.prod.outlook.com (2603:10a6:150:7d::22)
by PAWPR08MB8933.eurprd08.prod.outlook.com (2603:10a6:102:33d::16)
with Microsoft SMTP Server (version=TLS1_2,
cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7982.16; Sat, 21 Sep
2024 03:44:20 +0000
Received: from GV2PR08MB8121.eurprd08.prod.outlook.com
([fe80::4cd3:da80:2532:daa0]) by GV2PR08MB8121.eurprd08.prod.outlook.com
([fe80::4cd3:da80:2532:daa0%2]) with mapi id 15.20.8005.010; Sat, 21 Sep 2024
03:44:20 +0000
From: Mikhail Kshevetskiy
To: Simon Glass , Tom Rini ,
Joe Hershberger ,
Ramon Fried ,
Mattijs Korpershoek ,
Mikhail Kshevetskiy ,
AKASHI Takahiro ,
Heinrich Schuchardt ,
Michal Simek , Anand Moon ,
Marek Vasut ,
Ilias Apalodimas ,
Masahisa Kojima ,
Sean Anderson , Baruch Siach ,
Yasuharu Shibata ,
Richard Weinberger , u-boot@lists.denx.de
Subject: [PATCH v7 13/13] net/httpd-upload: an example web-server
implementation for file uploading
Date: Sat, 21 Sep 2024 06:43:53 +0300
Message-ID: <20240921034353.1298452-14-mikhail.kshevetskiy@iopsys.eu>
X-Mailer: git-send-email 2.45.2
In-Reply-To: <20240921034353.1298452-1-mikhail.kshevetskiy@iopsys.eu>
References: <20240921034353.1298452-1-mikhail.kshevetskiy@iopsys.eu>
X-ClientProxiedBy: GV2PEPF00004595.SWEP280.PROD.OUTLOOK.COM
(2603:10a6:158:401::440) To GV2PR08MB8121.eurprd08.prod.outlook.com
(2603:10a6:150:7d::22)
MIME-Version: 1.0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: GV2PR08MB8121:EE_|PAWPR08MB8933:EE_
X-MS-Office365-Filtering-Correlation-Id: 6905d065-c41a-4329-3d69-08dcd9efad07
X-MS-Exchange-SenderADCheck: 1
X-MS-Exchange-AntiSpam-Relay: 0
X-Microsoft-Antispam: BCL:0;
ARA:13230040|376014|1800799024|7416014|366016|52116014|38350700014|921020;
X-Microsoft-Antispam-Message-Info:
SNWEKUJ8/h/jDKO5oD2Yi9xjM8KuPELLn/p1CxCVd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L5am8+SOBlY/c9+LjiL8YqsYOMo9jdu+01zXLeNs4t8Q3zrZJHa73wbmqUO/4iMGg+kaX56S8IBx3189viRMgdk+8ePAiCwpwq1OgvY8+ekk+FKjnoOzdeNRDXy8XU7JsZA2NwIhZgL11nI/9zB0HsfesNMfzm21wZMdG8DdPW1b7At34moXPvFtX20eemBW4d9y5+CFSOYgDH3TPC98FNrXxLC1rUlqLZuXm47ypjgiuN/speceJsaGyRs2PG5efVY4HVsn8nad/B9dMEBTSNFNL1G6R1BmOEuTIffz7QxztBI5Zc/ndS+PzUh2A4oIk/9RqxEA+q3w47ikASx1w9Q9vOI/RYhsWjHg=
X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:;
IPV:NLI; SFV:NSPM; H:GV2PR08MB8121.eurprd08.prod.outlook.com; PTR:; CAT:NONE;
SFS:(13230040)(376014)(1800799024)(7416014)(366016)(52116014)(38350700014)(921020);
DIR:OUT; SFP:1102;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0:
2Tze4gOJwxe05zV87u+eaLxAcKrQrkSmx63RN82QZ1hmRTSpdIv4fevdMXmZ3ZaYHbNlx14H1TZTe2VQGIDbu2nrKbso4GUdzXxI3t7qaoQTLgwvXNy66k1N5DaeZngrDhih4/7lviB5Bz6UCyaPpy+DUpuV1miEUFlv4AxDd36DDm4pVLCNTe9QzgNwHmQ3ttaBqV4oSMBGs7N5vSop59pIL0CbJ/xESL9TsSKv3k80sFKcjdQswxwJ+RHKuRmqV0UilP8JIR2ncYA9zdE55/hvCx6o7jiBe5ulmgtAxdiVh3WGySpEba2HsGoT1ymy2Q9chpfu7Z5SYl709UlIHYG50OWkYn9Bz2MRtXpMfJKWYpkBisjzX/YqC5D5AH+6vvpMfVVxWZHnzIHKuiwPbSEcgsBJg8QL5gyP0kX1AZdPg0p5ex+1TSqMWrKu0w7DPW3IJm1x8zQKlxqTZovnx8V50D/0rgac93w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+4mnKM3BhBfDWEUZ//0A+wAzBYQ+QwgrQLrgfwpn+2R4OC/X48MsFiBddk/S0PNDQHzJJDvX6PNQ=
X-OriginatorOrg: iopsys.eu
X-MS-Exchange-CrossTenant-Network-Message-Id:
6905d065-c41a-4329-3d69-08dcd9efad07
X-MS-Exchange-CrossTenant-AuthSource: GV2PR08MB8121.eurprd08.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2024 03:44:20.4515 (UTC)
X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted
X-MS-Exchange-CrossTenant-Id: 8d891be1-7bce-4216-9a99-bee9de02ba58
X-MS-Exchange-CrossTenant-MailboxType: HOSTED
X-MS-Exchange-CrossTenant-UserPrincipalName:
FINV69XGKGUJYfyrtPz/EUqbcWMknVmJtcoDOawFDpey4ivGWlU0LIrEJEHp9u+A8+BAYWwPyGJ/att5JoR1fClJD1B7+hftIdPF1TaD/Pc=
X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAWPR08MB8933
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 is an example web-server implementation. It can be used for files
uploading to u-boot using a web-browser. It acts much like tftpget, but no
special servers needs to be installed by the user.
This code can be used as a base for other implementations like firmware
upgrade web-server used by some vendors.
Usage:
u-boot: start the we-server using the "httpd_upload" command
PC: open the "http://your_uboot_ip" link in the browser
Signed-off-by: Mikhail Kshevetskiy
Reviewed-by: Simon Glass
---
cmd/Kconfig | 25 ++++++
cmd/net.c | 20 +++++
include/net/httpd-upload.h | 12 +++
net/Makefile | 1 +
net/httpd-upload.c | 170 +++++++++++++++++++++++++++++++++++++
5 files changed, 228 insertions(+)
create mode 100644 include/net/httpd-upload.h
create mode 100644 net/httpd-upload.c
diff --git a/cmd/Kconfig b/cmd/Kconfig
index cfdc53e494a..b7749525ae3 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -2017,6 +2017,31 @@ config CMD_NETCAT
will be lost or reordered. Any netcat implementation should work,
but openbsd one was tested only.
+config CMD_HTTPD_UPLOAD
+ bool "an example HTTP server for file uploading"
+ depends on HTTPD_COMMON
+ help
+ An example HTTP/1.1 compatible web-server implementation for file
+ uploading. It acts much like tftpboot command: put user data to a
+ specified memory location, but no special tools needs to be installed
+ on the user side. The only required tool is browser.
+
+ Start 'httpd_upload' command, open a browser, connect to the board IP,
+ select file to upload and press 'Upload' button. This is enougth.
+
+ There is no big profit from this code, but it can be used as a
+ reference for other web-server implementations (ex: web-based
+ firmware upgrade/recovery used by some router vendors)
+
+config CMD_HTTPD_UPLOAD_MAX_SIZE
+ int "Maximum uploading size"
+ depends on CMD_HTTPD_UPLOAD
+ default 209715200
+ help
+ This option sets the resriction on the size of any uploaded file.
+ Please reserve 2--4 Kb more space due to additional space required
+ for storing of multipart/form-data header and footer.
+
config CMD_MII
bool "mii"
imply CMD_MDIO
diff --git a/cmd/net.c b/cmd/net.c
index 79bb126dbd4..f2df1eed2ef 100644
--- a/cmd/net.c
+++ b/cmd/net.c
@@ -21,6 +21,9 @@
#include
#include
#include
+#if defined(CONFIG_CMD_HTTPD_UPLOAD)
+#include
+#endif
static int netboot_common(enum proto_t, struct cmd_tbl *, int, char * const []);
@@ -229,6 +232,23 @@ U_BOOT_CMD_WITH_SUBCMDS(netcat,
U_BOOT_SUBCMD_MKENT(save, 3, 0, do_netcat_save));
#endif
+#if defined(CONFIG_CMD_HTTPD_UPLOAD)
+static int do_httpd_upload(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+ if (argc < 2)
+ return 1;
+
+ httpd_upload_prepare();
+ return netboot_common(HTTPD, cmdtp, argc, argv);
+}
+
+U_BOOT_CMD(
+ httpd_upload, 2, 1, do_httpd_upload,
+ "starts httpd server for file uploading",
+ "[loadAddress]\n"
+);
+#endif
+
static void netboot_update_env(void)
{
char tmp[46];
diff --git a/include/net/httpd-upload.h b/include/net/httpd-upload.h
new file mode 100644
index 00000000000..a80df214668
--- /dev/null
+++ b/include/net/httpd-upload.h
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: BSD-2-Clause
+ *
+ * httpd-upload include file
+ * Copyright (C) 2024 IOPSYS Software Solutions AB
+ * Author: Mikhail Kshevetskiy
+ */
+#ifndef __NET_HTTPD_UPLOAD_TCP_H__
+#define __NET_HTTPD_UPLOAD_TCP_H__
+
+void httpd_upload_prepare(void);
+
+#endif /* __NET_HTTPD_UPLOAD_TCP_H__ */
diff --git a/net/Makefile b/net/Makefile
index c1f491fad02..c1b41240aab 100644
--- a/net/Makefile
+++ b/net/Makefile
@@ -35,6 +35,7 @@ obj-$(CONFIG_PROT_TCP) += tcp.o
obj-$(CONFIG_CMD_WGET) += wget.o
obj-$(CONFIG_CMD_NETCAT) += netcat.o
obj-$(CONFIG_HTTPD_COMMON) += httpd.o
+obj-$(CONFIG_CMD_HTTPD_UPLOAD) += httpd-upload.o
# Disable this warning as it is triggered by:
# sprintf(buf, index ? "foo%d" : "foo", index)
diff --git a/net/httpd-upload.c b/net/httpd-upload.c
new file mode 100644
index 00000000000..8e43860fa74
--- /dev/null
+++ b/net/httpd-upload.c
@@ -0,0 +1,170 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * httpd-upload support driver
+ * Copyright (C) 2024 IOPSYS Software Solutions AB
+ * Author: Mikhail Kshevetskiy
+ */
+
+#include
+#include
+#include
+#include
+
+#define MAX_FILE_SIZE CONFIG_CMD_HTTPD_UPLOAD_MAX_SIZE
+
+static enum net_loop_state httpd_on_stop(void);
+
+static enum httpd_req_check httpd_pre_post(void *req_id, const char *url,
+ struct httpd_post_data *post);
+static struct http_reply *httpd_get(void *req_id, const char *url);
+static struct http_reply *httpd_post(void *req_id, const char *url,
+ struct httpd_post_data *post);
+static void httpd_on_req_end(void *req_id);
+
+static unsigned char error_400_html[] =
+ "\n"
+ " Bad request\n"
+ " \n"
+ " 400 - Bad Request
\n"
+ " The request you are trying to do is wrong!
\n"
+ " \n"
+ "\n";
+
+static unsigned char error_404_html[] =
+ "\n"
+ " Page not found\n"
+ " \n"
+ " 404 - Page not found
\n"
+ " The page you were looking for doesn't exist!
\n"
+ " \n"
+ "\n";
+
+static unsigned char index_html[] =
+ "\n"
+ " Upload File\n"
+ " \n"
+ " Upload File.
\n"
+ " \n"
+ " This will write the uploaded file to the memory area pointed\n"
+ " by ${loadaddr}.\n"
+ "
\n"
+ "
\n"
+ " \n"
+ "\n";
+
+static unsigned char upload_ok_html[] =
+ "\n"
+ " OK\n"
+ " \n"
+ " Upload OK
\n"
+ " The file was uploaded.
\n"
+ " \n"
+ "\n";
+
+static struct http_reply error_400 = {
+ .code = 400,
+ .code_msg = "Bad Request",
+ .data_type = "text/html; charset=utf-8",
+ .data = error_400_html,
+ .len = sizeof(error_400_html)
+};
+
+static struct http_reply error_404 = {
+ .code = 404,
+ .code_msg = "Not Found",
+ .data_type = "text/html; charset=utf-8",
+ .data = error_404_html,
+ .len = sizeof(error_404_html)
+};
+
+static struct http_reply index = {
+ .code = 200,
+ .code_msg = "OK",
+ .data_type = "text/html; charset=utf-8",
+ .data = index_html,
+ .len = sizeof(index_html)
+};
+
+static struct http_reply upload_ok = {
+ .code = 200,
+ .code_msg = "OK",
+ .data_type = "text/html; charset=utf-8",
+ .data = upload_ok_html,
+ .len = sizeof(upload_ok_html)
+};
+
+static struct httpd_config cfg = {
+ .on_stop = httpd_on_stop,
+ .on_req_end = httpd_on_req_end,
+ .get = httpd_get,
+ .post = httpd_post,
+ .pre_post = httpd_pre_post,
+ .error_400 = &error_400,
+ .error_404 = &error_404,
+};
+
+static enum net_loop_state httpd_loop_state;
+static void *post_req_id;
+
+void httpd_upload_prepare(void)
+{
+ httpd_setup(&cfg);
+ httpd_loop_state = NETLOOP_FAIL;
+}
+
+static enum httpd_req_check httpd_pre_post(void *req_id, const char *url,
+ struct httpd_post_data *post)
+{
+ if (post->size > MAX_FILE_SIZE) {
+ printf("HTTPD: reset connection, upload file is too large\n");
+ return HTTPD_CLNT_RST;
+ }
+
+ post_req_id = req_id;
+ return HTTPD_REQ_OK;
+}
+
+static struct http_reply *httpd_post(void *req_id, const char *url,
+ struct httpd_post_data *post)
+{
+ if (strcmp(url, "/file_upload"))
+ return &error_404;
+
+ httpd_loop_state = NETLOOP_SUCCESS;
+ printf("HTTPD: upload OK\n");
+ return &upload_ok;
+}
+
+static struct http_reply *httpd_get(void *req_id, const char *url)
+{
+ if (!strcmp(url, "/"))
+ return &index;
+ if (!strcmp(url, "/index.html"))
+ return &index;
+ return &error_404;
+}
+
+static void httpd_on_req_end(void *req_id)
+{
+ if (req_id == post_req_id) {
+ post_req_id = NULL;
+ httpd_stop();
+ }
+}
+
+static enum net_loop_state httpd_on_stop(void)
+{
+ return httpd_loop_state;
+}