From patchwork Thu Apr 16 17:59:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Daniel M. Weeks" X-Patchwork-Id: 1271845 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4936dr0mYfz9sWY for ; Fri, 17 Apr 2020 04:08:00 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=rpi.edu Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 4936dp1wblzDrhm for ; Fri, 17 Apr 2020 04:07:58 +1000 (AEST) X-Original-To: petitboot@lists.ozlabs.org Delivered-To: petitboot@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=rpi.edu (client-ip=128.113.2.230; helo=smtp10.server.rpi.edu; envelope-from=weeksd2@rpi.edu; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=rpi.edu Received: from smtp10.server.rpi.edu (smtp10.server.rpi.edu [128.113.2.230]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4936SV4Kp4zDsG2 for ; Fri, 17 Apr 2020 03:59:54 +1000 (AEST) Received: from smtp-auth3.server.rpi.edu (route.canit.rpi.edu [128.113.2.233]) by smtp10.server.rpi.edu (8.14.4/8.14.4/Debian-8+deb8u2) with ESMTP id 03GHxo9b096964 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 16 Apr 2020 13:59:50 -0400 Received: from smtp-auth3.server.rpi.edu (localhost [127.0.0.1]) by smtp-auth3.server.rpi.edu (Postfix) with ESMTP id 4E40558045 for ; Thu, 16 Apr 2020 13:59:50 -0400 (EDT) Received: from dev.danweeks.net (cpe-74-70-107-6.nycap.res.rr.com [74.70.107.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: weeksd2) by smtp-auth3.server.rpi.edu (Postfix) with ESMTPSA id 2E1F35800F for ; Thu, 16 Apr 2020 13:59:50 -0400 (EDT) Date: Thu, 16 Apr 2020 13:59:48 -0400 From: "Daniel M. Weeks" To: petitboot@lists.ozlabs.org Subject: [PATCH 08/11] New function to convert pretty hwaddr to raw Message-ID: <9e7aba1f603262afc64e3d607c54e0220dc8e329.1587059235.git.weeksd2@rpi.edu> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Virus-Scanned: ClamAV using ClamSMTP X-Bayes-Prob: 0.0001 (Score 0, tokens from: outgoing, @@RPTN) X-Spam-Score: 0.00 () [Hold at 10.10] X-CanIt-Incident-Id: 032rtXOJU X-CanIt-Geo: ip=74.70.107.6; country=US; region=New York; city=Troy; latitude=42.7273; longitude=-73.6696; http://maps.google.com/maps?q=42.7273,-73.6696&z=6 X-CanItPRO-Stream: outgoing X-Canit-Stats-ID: Bayes signature not available X-Scanned-By: CanIt (www . roaringpenguin . com) on 128.113.2.230 X-BeenThere: petitboot@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Petitboot bootloader development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: petitboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Petitboot" This function extracts a method used in several places to convert a pretty hardware address back to raw bytes. It also includes support for an 8-byte hardware address. Signed-off-by: Daniel M. Weeks --- lib/util/util.c | 25 +++++++++++++++++++++++++ lib/util/util.h | 2 ++ 2 files changed, 27 insertions(+) diff --git a/lib/util/util.c b/lib/util/util.c index c965cc4..7f70084 100644 --- a/lib/util/util.c +++ b/lib/util/util.c @@ -15,6 +15,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include #include #include @@ -82,3 +83,27 @@ const char *hwaddr_preprocess(const char *mac, size_t *length) return NULL; } } + +int hwaddr_from_pretty_str(const char *hwaddr_str, size_t hwaddr_len, uint8_t *hwaddr, size_t hwaddr_size) +{ + if (hwaddr_size < hwaddr_len) + return -1; + + if (hwaddr_len == 6) { + sscanf(hwaddr_str, + "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX", + hwaddr, hwaddr + 1, hwaddr + 2, + hwaddr + 3, hwaddr + 4, hwaddr + 5); + } else if (hwaddr_len == 8) { + sscanf(hwaddr_str, + "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX:%hhX:%hhX", + hwaddr, hwaddr + 1, hwaddr + 2, + hwaddr + 3, hwaddr + 4, hwaddr + 5, + hwaddr + 6, hwaddr + 7); + } else { + pb_debug("unsupported hwaddr format\n"); + return -1; + } + + return 0; +} diff --git a/lib/util/util.h b/lib/util/util.h index d9cc46b..bbb6144 100644 --- a/lib/util/util.h +++ b/lib/util/util.h @@ -56,5 +56,7 @@ int hwaddr_cmp(uint8_t *hwaddr1, size_t hwaddr1_len, uint8_t *hwaddr2, size_t hw const char *hwaddr_preprocess(const char *mac, size_t *length); +int hwaddr_from_pretty_str(const char *hwaddr_str, size_t hwaddr_len, uint8_t *hwaddr, size_t hwaddr_size); + #endif /* UTIL_H */