From patchwork Fri Nov 26 16:17:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 1560214 X-Patchwork-Delegate: zajec5@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=YP5MsbWJ; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=Jdb/l/O+; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4J10RG6W3Nz9s1l for ; Sat, 27 Nov 2021 03:23:22 +1100 (AEDT) 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:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=Ne5iGT4VFKMMoxTl3I7rjigr/j88ktW6y2/gNcKYGDQ=; b=YP5MsbWJRJWKi3 GyrzntKnc9/yYcY71pi9kIzD6j/3st35GojoNI3vOBSITf1vlMVbAmgzuAOmbmUIzvr9/DSj/kkyg qHbRwBnU4RPlFwQtqjlbk00wQgVXcF8ky1yCa+MgE1CZ5z3nj2m/5wqmhwMASysF/nt6+qfEKP+C0 Rh8SrKpVnUzU2k87my0ey9itF/SQ0VaPCyNAZfS/2G91H2UnCSRNIbDo/qcNfByPAI0I0UgqPNbaU EUbl0cJTWfkDecwd/30ZYDVOALWbiMfA7jvT0L/Cn0TJ9USz6lG+p+jEfak2TRzCHpmaLnAdaUbTD DIhMzZn/5F9+gkehmDxg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mqdua-00B6YZ-0Y; Fri, 26 Nov 2021 16:17:20 +0000 Received: from mail-lj1-x234.google.com ([2a00:1450:4864:20::234]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mqduV-00B6X4-JX for openwrt-devel@lists.openwrt.org; Fri, 26 Nov 2021 16:17:17 +0000 Received: by mail-lj1-x234.google.com with SMTP id k23so19786221lje.1 for ; Fri, 26 Nov 2021 08:17:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=jME1q0nUiROjMmpzcpYvq4XnsrxOL8ogdLQTV/a08ag=; b=Jdb/l/O+lRq+TFU4J36xnv6LmqO2R05/+rVkWlcf00ZmEOkijuqkGG2ZiUse26Fx5K pE4CynPEa3w0fHoyjrYcviFR5oYPpzbG1J6GJ/xajE8pB6WdVMdCTIEZRu+Ww9C9Gppv K5thSRBV/laKE3HBLgc9s7sIsJZqalgo7J2q6RGexrOb/yZ3zu8OBYHBNiPaCgLwjTwV b9ij5zOK880SIUGT0xwx+TGDzEm07IdrQo5ZBvbBAULVE3MNAdLIJ77lVH3cJI/6lPeg bzg4DCoRlOykA68CNzyjL1R2CcG49wZB9NxeVci3gmFGZRFgVVAZ5MyirYePbhESxvC/ yd9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=jME1q0nUiROjMmpzcpYvq4XnsrxOL8ogdLQTV/a08ag=; b=yu3WHWpforq+0YDv64C8cG1DIyOqeeWJS/BU9lRKvvoLazsOQkMfFBd2in8zMfBybc IFjuvEnm//ZNgr/eUgNh+l7kMI4ndf7RLjmMb6V1Q6vejH6p2YIgCgHT4fncu8TZcW7a TCJeYCOVWQffbst3CmkycA/kSgzGpyY9r+GTFVpXC4trw36Gxzbeh6TTXATywO8JVWdp WObKkr9/vRQB1X0fZrpLqhb4GEdEGWVkdwX00YDHlqP7RbzdFV17+EHunV2EyS41MJLH CMcUGxyw+lr7Hk4wXwK4MMrVa6j2GPKOhS6p/rBny4htww+1b+ZCfU782pe2gkJ1/MUH Ck9g== X-Gm-Message-State: AOAM530Cuu1I07NeD70kgsngnDApQEdn529W1JfoUYTJ/hiyKezXq2yC obLE1Aa0FLaZ/rMg0iOLnRTg5HhKh1Q= X-Google-Smtp-Source: ABdhPJxhQgMIyJ8JV5lvK4S2FgPSqkUYVLoYS4v1C6Z4WCl9qV8xBhESaEVnyxJ20BLPTJyCSwY9NA== X-Received: by 2002:a2e:b4a6:: with SMTP id q6mr31159303ljm.7.1637943433771; Fri, 26 Nov 2021 08:17:13 -0800 (PST) Received: from localhost.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by smtp.gmail.com with ESMTPSA id j19sm612482lfe.120.2021.11.26.08.17.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Nov 2021 08:17:13 -0800 (PST) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: openwrt-devel@lists.openwrt.org Cc: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH firmware-utils 1/2] oseama: allow reading from stdin Date: Fri, 26 Nov 2021 17:17:07 +0100 Message-Id: <20211126161708.9306-1-zajec5@gmail.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211126_081715_687587_C3BA998C X-CRM114-Status: GOOD ( 14.79 ) X-Spam-Score: 0.1 (/) X-Spam-Report: =?unknown-8bit?q?Spam_detection_software=2C_running_on_the_sy?= =?unknown-8bit?q?stem_=22bombadil=2Einfradead=2Eorg=22=2C?= =?unknown-8bit?q?_has_NOT_identified_this_incoming_email_as_spam=2E__The_ori?= =?unknown-8bit?q?ginal?= =?unknown-8bit?q?_message_has_been_attached_to_this_so_you_can_view_it_or_la?= =?unknown-8bit?q?bel?= =?unknown-8bit?q?_similar_future_email=2E__If_you_have_any_questions=2C_see?= =?unknown-8bit?q?_the_administrator_of_that_system_for_details=2E?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_Content_preview=3A__From=3A_Rafa=C5=82_Mi=C5=82ecki_This_a?= =?unknown-8bit?q?dds_support_for_reading_Seama?= =?unknown-8bit?q?_seal_from_stdin_when_using_=22info=22_or_=22extract=22_com?= =?unknown-8bit?q?mand=2E_It_allows_e=2Eg=2E_cat?= =?unknown-8bit?q?_firmware=2Ebin_=7C_oseama_info_-_?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_Content_analysis_details=3A___=280=2E1_points=2C_5=2E0_req?= =?unknown-8bit?q?uired=29?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_pts_rule_name______________description?= =?unknown-8bit?q?_----_----------------------_------------------------------?= =?unknown-8bit?q?--------------------?= =?unknown-8bit?q?_-0=2E0_RCVD=5FIN=5FDNSWL=5FNONE_____RBL=3A_Sender_listed_a?= =?unknown-8bit?q?t_https=3A//www=2Ednswl=2Eorg/=2C?= =?unknown-8bit?q?_no_trust?= =?unknown-8bit?b?IFsyYTAwOjE0NTA6NDg2NDoyMDowOjA6MDoyMzQgbGlzdGVkIGluXQ==?= =?unknown-8bit?b?IFtsaXN0LmRuc3dsLm9yZ10=?= =?unknown-8bit?q?_-0=2E0_SPF=5FPASS_______________SPF=3A_sender_matches_SPF_?= =?unknown-8bit?q?record?= =?unknown-8bit?q?_0=2E0_SPF=5FHELO=5FNONE__________SPF=3A_HELO_does_not_publ?= =?unknown-8bit?q?ish_an_SPF_Record?= =?unknown-8bit?q?_0=2E0_FREEMAIL=5FFROM__________Sender_email_is_commonly_ab?= =?unknown-8bit?q?used_enduser_mail?= =?unknown-8bit?q?_provider?= =?unknown-8bit?q?_=5Bzajec5=5Bat=5Dgmail=2Ecom=5D?= =?unknown-8bit?q?_0=2E2_FREEMAIL=5FENVFROM=5FEND=5FDIGIT_Envelope-from_freem?= =?unknown-8bit?q?ail_username_ends?= =?unknown-8bit?q?_in_digit?= =?unknown-8bit?q?_=5Bzajec5=5Bat=5Dgmail=2Ecom=5D?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID=5FAU__________Message_has_a_valid_DKIM?= =?unknown-8bit?q?_or_DK_signature_from?= =?unknown-8bit?q?_author=27s_domain?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID_____________Message_has_at_least_one_v?= =?unknown-8bit?q?alid_DKIM_or_DK_signature?= =?unknown-8bit?q?_0=2E1_DKIM=5FSIGNED____________Message_has_a_DKIM_or_DK_si?= =?unknown-8bit?q?gnature=2C_not_necessarily?= =?unknown-8bit?q?_valid?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID=5FEF__________Message_has_a_valid_DKIM?= =?unknown-8bit?q?_or_DK_signature_from?= =?unknown-8bit?q?_envelope-from_domain?= X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org From: Rafał Miłecki This adds support for reading Seama seal from stdin when using "info" or "extract" command. It allows e.g. cat firmware.bin | oseama info - Signed-off-by: Rafał Miłecki --- src/oseama.c | 77 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 64 insertions(+), 13 deletions(-) diff --git a/src/oseama.c b/src/oseama.c index 75580e6..d3eb9b9 100644 --- a/src/oseama.c +++ b/src/oseama.c @@ -59,6 +59,44 @@ static inline size_t oseama_min(size_t x, size_t y) { return x < y ? x : y; } +/************************************************** + * Helpers + **************************************************/ + +static FILE *oseama_open(const char *pathname, const char *mode) { + if (strcmp(pathname, "-")) + return fopen(pathname, mode); + + if (isatty(fileno(stdin))) { + fprintf(stderr, "Reading from TTY stdin is unsupported\n"); + return NULL; + } + + return stdin; +} + +static int oseama_skip(FILE *fp, size_t length) +{ + if (fseek(fp, length, SEEK_CUR)) { + uint8_t buf[1024]; + size_t bytes; + + do { + bytes = fread(buf, 1, oseama_min(sizeof(buf), length), fp); + if (bytes <= 0) + return -EIO; + length -= bytes; + } while (length); + } + + return 0; +} + +static void oseama_close(FILE *fp) { + if (fp != stdin) + fclose(fp); +} + /************************************************** * Info **************************************************/ @@ -75,7 +113,7 @@ static void oseama_info_parse_options(int argc, char **argv) { } } -static int oseama_info_entities(FILE *seama) { +static int oseama_info_entities(FILE *seama, size_t *pos) { struct seama_entity_header hdr; size_t bytes, metasize, imagesize; uint8_t buf[1024]; @@ -84,6 +122,8 @@ static int oseama_info_entities(FILE *seama) { int err = 0; while ((bytes = fread(&hdr, 1, sizeof(hdr), seama)) == sizeof(hdr)) { + *pos += bytes; + if (be32_to_cpu(hdr.magic) != SEAMA_MAGIC) { fprintf(stderr, "Invalid Seama magic: 0x%08x\n", be32_to_cpu(hdr.magic)); err = -EINVAL; @@ -93,7 +133,8 @@ static int oseama_info_entities(FILE *seama) { imagesize = be32_to_cpu(hdr.imagesize); if (entity_idx >= 0 && i != entity_idx) { - fseek(seama, metasize + imagesize, SEEK_CUR); + oseama_skip(seama, metasize + imagesize); + *pos += metasize + imagesize; i++; continue; } @@ -106,7 +147,7 @@ static int oseama_info_entities(FILE *seama) { if (entity_idx < 0) printf("\n"); - printf("Entity offset:\t%ld\n", ftell(seama) - sizeof(hdr)); + printf("Entity offset:\t%ld\n", *pos - sizeof(hdr)); printf("Entity size:\t%zd\n", sizeof(hdr) + metasize + imagesize); printf("Meta size:\t%zd\n", metasize); printf("Image size:\t%zd\n", imagesize); @@ -117,6 +158,7 @@ static int oseama_info_entities(FILE *seama) { err = -EIO; goto err_out; } + *pos += bytes; end = (char *)&buf[metasize - 1]; *end = '\0'; @@ -124,7 +166,8 @@ static int oseama_info_entities(FILE *seama) { printf("Meta entry:\t%s\n", tmp); } - fseek(seama, imagesize, SEEK_CUR); + oseama_skip(seama, imagesize); + *pos += imagesize; i++; } @@ -139,6 +182,7 @@ static int oseama_info(int argc, char **argv) { uint16_t metasize; uint32_t imagesize; uint8_t buf[1024]; + size_t pos = 0; int err = 0; if (argc < 3) { @@ -151,7 +195,7 @@ static int oseama_info(int argc, char **argv) { optind = 3; oseama_info_parse_options(argc, argv); - seama = fopen(seama_path, "r"); + seama = oseama_open(seama_path, "r"); if (!seama) { fprintf(stderr, "Couldn't open %s\n", seama_path); err = -EACCES; @@ -164,6 +208,8 @@ static int oseama_info(int argc, char **argv) { err = -EIO; goto err_close; } + pos += bytes; + metasize = be16_to_cpu(hdr.metasize); imagesize = be32_to_cpu(hdr.imagesize); @@ -191,6 +237,7 @@ static int oseama_info(int argc, char **argv) { err = -EIO; goto err_close; } + pos += bytes; if (entity_idx < 0) { char *end, *tmp; @@ -205,10 +252,10 @@ static int oseama_info(int argc, char **argv) { } } - oseama_info_entities(seama); + oseama_info_entities(seama, &pos); err_close: - fclose(seama); + oseama_close(seama); out: return err; } @@ -424,14 +471,18 @@ static int oseama_extract_entity(FILE *seama, FILE *out) { imagesize = be32_to_cpu(hdr.imagesize); if (i != entity_idx) { - fseek(seama, metasize + imagesize, SEEK_CUR); + oseama_skip(seama, metasize + imagesize); i++; continue; } - fseek(seama, -sizeof(hdr), SEEK_CUR); + if (fwrite(&hdr, 1, sizeof(hdr), out) != sizeof(hdr)) { + fprintf(stderr, "Couldn't write %zu B to %s\n", sizeof(hdr), out_path); + err = -EIO; + break; + } - length = sizeof(hdr) + metasize + imagesize; + length = metasize + imagesize; while ((bytes = fread(buf, 1, oseama_min(sizeof(buf), length), seama)) > 0) { if (fwrite(buf, 1, bytes, out) != bytes) { fprintf(stderr, "Couldn't write %zu B to %s\n", bytes, out_path); @@ -480,7 +531,7 @@ static int oseama_extract(int argc, char **argv) { goto out; } - seama = fopen(seama_path, "r"); + seama = oseama_open(seama_path, "r"); if (!seama) { fprintf(stderr, "Couldn't open %s\n", seama_path); err = -EACCES; @@ -502,14 +553,14 @@ static int oseama_extract(int argc, char **argv) { } metasize = be16_to_cpu(hdr.metasize); - fseek(seama, metasize, SEEK_CUR); + oseama_skip(seama, metasize); oseama_extract_entity(seama, out); err_close_out: fclose(out); err_close_seama: - fclose(seama); + oseama_close(seama); out: return err; } From patchwork Fri Nov 26 16:17:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 1560213 X-Patchwork-Delegate: zajec5@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=XyPEde2S; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=Re6ZFPpa; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4J10RG75CSz9t54 for ; Sat, 27 Nov 2021 03:23:22 +1100 (AEDT) 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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=vC8pdfi3nOaRGQsvQCtQsTjjEERwXlbjdrVopcoz6dE=; b=XyPEde2SzNzEbx YAA6QLg5CHaCGcGbdlKUKlHTUusxVxQi63h2I2MS7Lj/L3U/Rw2n0/Hhn4l1DRc+HgN2cPKC+2scX vvec22sz8wUF5crPMCXC1NhQnnnXskx+Vh1/Lzx3qVhXDSszpKtB7UL0HyUGNSXqJNd8h6vewf00W HTSXV4hRpYMBPWeqRB5uvACPrr+lftB9u/EEZ1KylJ81dUY94VKA2nA8jUjLZ1spnRyeQxF/FFQRR mUx7i+4vn5vmeChEnhKqfmtY7p45X5cTJuOHvdE5Jx95Hg1boH3HIJsRkcWSaKtMlFr+kCAR4n02c hw0kNccHJXsMQomiCHyQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mqdum-00B6a2-Ik; Fri, 26 Nov 2021 16:17:32 +0000 Received: from mail-lf1-x129.google.com ([2a00:1450:4864:20::129]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mqdub-00B6Xj-Hf for openwrt-devel@lists.openwrt.org; Fri, 26 Nov 2021 16:17:22 +0000 Received: by mail-lf1-x129.google.com with SMTP id n12so25534185lfe.1 for ; Fri, 26 Nov 2021 08:17:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=m+h7Pz/7jg4dMdMlBsYW+Td79I9X8BRSW2ZST0OQxlA=; b=Re6ZFPpa+oc2gfGj+i9Cni5oQsRkhUp3TcOnrkFsh1qbnnvY9cRe3EgyhQAxy9uUx8 iQ6OmTRmRMwMvbUbHq/ne2IMeILWDmQBk+J4GRKDcc13qkxhj3FJaXJfEoWljmUvFb2Q 6jK/Y+6T3Y3qYgcYwQ0B3IJFiZJ1SQ+0+uL6H0g7P6SFX2MVxn8xuz4r4gwfChhsEbd5 Yodb6nbBNVscPDtmiZR9eKsfSVD+MXCFs21/eBV6HoVWXkKbqzkT+yE0KFVh85rpA5O/ 8m1JHPkHJmgkTSVtl7xdINwffbdEkUCFKy/BKr39xsOE19Odyp25ZIWQ2WBaXzla9+uw ebBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=m+h7Pz/7jg4dMdMlBsYW+Td79I9X8BRSW2ZST0OQxlA=; b=uwVF9Z1r/elbBlVTYxCYYhPHKSFqabwN0Y06KLAN4WofxnBdiu7rHEv+tovbda/+w/ 3FqHYXlp3X+s6TvcRhyQ90tPdLxVPW7VTF40YdKQXfVK1f1B6yNI8k0Y375EPwLrAuum 02BeBb161VFSKVTLozdRZkXqeeJgtu0ULtSDTvfqrlI+SxvCO8LYFGkDt0DPf+mizgbg 0D5ZLjGqsyFfWEVbzQDbn46+DzjwskpgXRz652kF9rul7tlDDHytRO1ILp4QzzzBWpp+ KmuVzBX9NG+p7ZWFrEqBQpcPxl4Tx4TVhltb1fc2eD36AO4wISYWaKKO/oJ9bhmMy9td HAtg== X-Gm-Message-State: AOAM530z49s3gbs5RJkdtyIohXjEAHIIvONK+b2hrxmSIGkiCoNIRX1S ZMa+omTCHBahI/Ds9zKL3u0lObkf3vM= X-Google-Smtp-Source: ABdhPJym28JK+ANppfNQXBoA88jr7JTFIrgLfzdgL+m1dg/yba0CjrhSmC7ToUfcbD0VdlK2lu2gWw== X-Received: by 2002:a19:4f02:: with SMTP id d2mr31176691lfb.547.1637943435298; Fri, 26 Nov 2021 08:17:15 -0800 (PST) Received: from localhost.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by smtp.gmail.com with ESMTPSA id j19sm612482lfe.120.2021.11.26.08.17.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Nov 2021 08:17:15 -0800 (PST) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: openwrt-devel@lists.openwrt.org Cc: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH firmware-utils 2/2] oseama: support extracting entity to stdout Date: Fri, 26 Nov 2021 17:17:08 +0100 Message-Id: <20211126161708.9306-2-zajec5@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211126161708.9306-1-zajec5@gmail.com> References: <20211126161708.9306-1-zajec5@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211126_081721_609035_5A312A85 X-CRM114-Status: GOOD ( 12.52 ) X-Spam-Score: 0.1 (/) X-Spam-Report: =?unknown-8bit?q?Spam_detection_software=2C_running_on_the_sy?= =?unknown-8bit?q?stem_=22bombadil=2Einfradead=2Eorg=22=2C?= =?unknown-8bit?q?_has_NOT_identified_this_incoming_email_as_spam=2E__The_ori?= =?unknown-8bit?q?ginal?= =?unknown-8bit?q?_message_has_been_attached_to_this_so_you_can_view_it_or_la?= =?unknown-8bit?q?bel?= =?unknown-8bit?q?_similar_future_email=2E__If_you_have_any_questions=2C_see?= =?unknown-8bit?q?_the_administrator_of_that_system_for_details=2E?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_Content_preview=3A__From=3A_Rafa=C5=82_Mi=C5=82ecki_This_a?= =?unknown-8bit?q?llows_using_oseama_in_a_pipe=2E?= =?unknown-8bit?q?_It=27s_very_useful_for_writing_data_to_flash_using_e=2Eg?= =?unknown-8bit?q?=2E_oseama_extract_firmware=2Ebin?= =?unknown-8bit?q?_-e_0_=7C_mtd_write_-_firmware_Signed-off-by=3A_Rafa=C5=82_?= =?unknown-8bit?q?Mi=C5=82ecki_---_src/oseama=2Ec?= =?unknown-8bit?q?_=7C_21_+++++++++++_1_file_changed=2C_11_insertions=28+=29?= =?unknown-8bit?q?=2C_10_deletions=28-=29_?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_Content_analysis_details=3A___=280=2E1_points=2C_5=2E0_req?= =?unknown-8bit?q?uired=29?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_pts_rule_name______________description?= =?unknown-8bit?q?_----_----------------------_------------------------------?= =?unknown-8bit?q?--------------------?= =?unknown-8bit?q?_-0=2E0_RCVD=5FIN=5FDNSWL=5FNONE_____RBL=3A_Sender_listed_a?= =?unknown-8bit?q?t_https=3A//www=2Ednswl=2Eorg/=2C?= =?unknown-8bit?q?_no_trust?= =?unknown-8bit?b?IFsyYTAwOjE0NTA6NDg2NDoyMDowOjA6MDoxMjkgbGlzdGVkIGluXQ==?= =?unknown-8bit?b?IFtsaXN0LmRuc3dsLm9yZ10=?= =?unknown-8bit?q?_-0=2E0_SPF=5FPASS_______________SPF=3A_sender_matches_SPF_?= =?unknown-8bit?q?record?= =?unknown-8bit?q?_0=2E0_SPF=5FHELO=5FNONE__________SPF=3A_HELO_does_not_publ?= =?unknown-8bit?q?ish_an_SPF_Record?= =?unknown-8bit?q?_0=2E0_FREEMAIL=5FFROM__________Sender_email_is_commonly_ab?= =?unknown-8bit?q?used_enduser_mail?= =?unknown-8bit?q?_provider?= =?unknown-8bit?q?_=5Bzajec5=5Bat=5Dgmail=2Ecom=5D?= =?unknown-8bit?q?_0=2E2_FREEMAIL=5FENVFROM=5FEND=5FDIGIT_Envelope-from_freem?= =?unknown-8bit?q?ail_username_ends?= =?unknown-8bit?q?_in_digit?= =?unknown-8bit?q?_=5Bzajec5=5Bat=5Dgmail=2Ecom=5D?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID=5FAU__________Message_has_a_valid_DKIM?= =?unknown-8bit?q?_or_DK_signature_from?= =?unknown-8bit?q?_author=27s_domain?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID_____________Message_has_at_least_one_v?= =?unknown-8bit?q?alid_DKIM_or_DK_signature?= =?unknown-8bit?q?_0=2E1_DKIM=5FSIGNED____________Message_has_a_DKIM_or_DK_si?= =?unknown-8bit?q?gnature=2C_not_necessarily?= =?unknown-8bit?q?_valid?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID=5FEF__________Message_has_a_valid_DKIM?= =?unknown-8bit?q?_or_DK_signature_from?= =?unknown-8bit?q?_envelope-from_domain?= X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org From: Rafał Miłecki This allows using oseama in a pipe. It's very useful for writing data to flash using e.g. oseama extract firmware.bin -e 0 | mtd write - firmware Signed-off-by: Rafał Miłecki --- src/oseama.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/oseama.c b/src/oseama.c index d3eb9b9..f68d7d0 100644 --- a/src/oseama.c +++ b/src/oseama.c @@ -525,10 +525,6 @@ static int oseama_extract(int argc, char **argv) { fprintf(stderr, "No entity specified\n"); err = -EINVAL; goto out; - } else if (!out_path) { - fprintf(stderr, "No output file specified\n"); - err = -EINVAL; - goto out; } seama = oseama_open(seama_path, "r"); @@ -538,11 +534,15 @@ static int oseama_extract(int argc, char **argv) { goto out; } - out = fopen(out_path, "w"); - if (!out) { - fprintf(stderr, "Couldn't open %s\n", out_path); - err = -EACCES; - goto err_close_seama; + if (out_path) { + out = fopen(out_path, "w"); + if (!out) { + fprintf(stderr, "Couldn't open %s\n", out_path); + err = -EACCES; + goto err_close_seama; + } + } else { + out = stdout; } bytes = fread(&hdr, 1, sizeof(hdr), seama); @@ -558,7 +558,8 @@ static int oseama_extract(int argc, char **argv) { oseama_extract_entity(seama, out); err_close_out: - fclose(out); + if (out != stdout) + fclose(out); err_close_seama: oseama_close(seama); out: