From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id B715DA0524; Wed, 2 Jun 2021 13:13:29 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 3F53D4069F; Wed, 2 Jun 2021 13:13:29 +0200 (CEST) Received: from mail-io1-f45.google.com (mail-io1-f45.google.com [209.85.166.45]) by mails.dpdk.org (Postfix) with ESMTP id D45EF40689 for ; Wed, 2 Jun 2021 13:13:28 +0200 (CEST) Received: by mail-io1-f45.google.com with SMTP id k16so2046797ios.10 for ; Wed, 02 Jun 2021 04:13:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=FH0ddXvfwAP2RyKOspgzPARuZQc9lZbfTQP5ihx85jE=; b=QzL373h8aO0s5AoV4BhA8fsewvOuv0dac/GVTVdodbUQFY7iMJwR4Gt5q5EA2tIqgL HicsVLEaxw9KVt1t5EpYYS0wsMMRkRZWbslMoUsZiVYTKSkLqyw4jbqNTd0J3bG0/LVZ 73JSzcMXfSfekqGRtp5h2gnpSA/dh3rzbjK/XfZMKkKz5wPMw6VLFa4qVThd9LlUpikO ur+cm9zxMYlvC7i15ArYOyyL5Cw6wWer+gquMBY39WTg9Y9mhoIeSUZ3p9zr323UhFbN EvN3XNrS9P9j7PVwgKfWbuFp7SHBg2GWqkR0r/GkoCm5eyjWZI/rhm7Oagg+VzKHmgrN nBrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=FH0ddXvfwAP2RyKOspgzPARuZQc9lZbfTQP5ihx85jE=; b=IJg3tkIuxBjVSZwIlOfBy0ez0QmDeDvAIMVJEVYP0yjJqpFrHl/M2iTxyNhCovGKUx Q5fI3OmxbwbrpBMpQeXISg55ziBznmJMilwSBTXY5qCGU8mNvLc21RZWCK6Tdupr3OVO xLjgT9HCXQm01Dl8KRr0+NzRk0ylalBqK6vnekGJprSXf2s+nfTnfcqDeg+riNkYIuvN EDbf+2VPb0vRELyLgQLAlzcRurFoCIWcNteynaQelC8iW6AwosPQK9UFxgtv/BcU8iev pPvub4E+05WfgVzqe1PAZb3JoxIAwCFAM0u5NFs5jz8smnDeE/99TBYYI+Ec4jh7BwKY Vong== X-Gm-Message-State: AOAM531anFLu/VKnlrHfG2pKyGs53TY95w8X03DVj5vf5QUU6KojenD8 cAguJGC60Edzehk1jKMAd9c4UGTVPyuM6ZQ2+Uw= X-Google-Smtp-Source: ABdhPJyR3rarU/0xf9NC4cs29FsPYYuYfi4EoZIkFgpD24Nd3IQsu+IfwXOPi4/+0/AyZC98XfVmSUxNPnsXaE4MasI= X-Received: by 2002:a6b:3e88:: with SMTP id l130mr1649262ioa.59.1622632408105; Wed, 02 Jun 2021 04:13:28 -0700 (PDT) MIME-Version: 1.0 References: <20210602095836.24901-1-david.marchand@redhat.com> <20210602095836.24901-3-david.marchand@redhat.com> In-Reply-To: <20210602095836.24901-3-david.marchand@redhat.com> From: Jerin Jacob Date: Wed, 2 Jun 2021 16:43:12 +0530 Message-ID: To: David Marchand Cc: dpdk-dev , Aaron Conole , Michael Santana , Bruce Richardson , Rasesh Mody , Shahed Shaikh , Qiming Yang , Qi Zhang , Heinrich Kuhn , Devendra Singh Rawat , Igor Russkikh , Ray Kinsella , Neil Horman , Dmitry Kozlyuk , Narcisa Ana Maria Vasile , Dmitry Malloy , Pallavi Kadam Content-Type: text/plain; charset="UTF-8" Subject: Re: [dpdk-dev] [PATCH 2/2] eal: handle compressed firmwares X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" On Wed, Jun 2, 2021 at 3:29 PM David Marchand wrote: > > Introduce an internal firmware loading helper to remove code duplication > in our drivers and handle xz compressed firmwares by calling libarchive. > > This helper tries to look for .xz suffixes so that drivers are not aware > the firmwares have been compressed. > > libarchive is set as an optional dependency: without libarchive, a > runtime warning is emitted so that users know there is a compressed > firmware. > > Windows implementation is left as an empty stub. > > Signed-off-by: David Marchand > + */ > + > +#ifndef __RTE_FIRMWARE_H__ > +#define __RTE_FIRMWARE_H__ > + > +#include > + > +#include > + > +/** > + * @warning > + * @b EXPERIMENTAL: this API may change without prior notice > + * > + * Load a firmware in a dynamically allocated buffer, dealing with compressed > + * files if libarchive is available. > + * > + * @param name > + * Firmware filename to load. > + * @param buf Adding out to express the output useful. i.e @param[out] buf > + * Buffer allocated by this function. If this function succeeds, the > + * caller is responsible for freeing the buffer. I think, we can chnange to "freeing the buffer using free()" to avoid confusion with rte_free() > + * @param bufsz @param[out] bufsz > + * Size of the data in the buffer. > + * > + * @return > + * 0 if successful. > + * Negative otherwise, buf and bufsize contents are invalid. > + */ > +__rte_internal > +int > +rte_firmware_read(const char *name, void **buf, size_t *bufsz); > + > +#endif > diff --git a/lib/eal/unix/eal_firmware.c b/lib/eal/unix/eal_firmware.c > new file mode 100644 > index 0000000000..ea66fecfe9 > --- /dev/null > +++ b/lib/eal/unix/eal_firmware.c > @@ -0,0 +1,106 @@ > +/* SPDX-License-Identifier: BSD-3-Clause > + * Copyright(c) 2021 Red Hat, Inc. > + */ > + > +#ifdef RTE_HAS_LIBARCHIVE > +#include > +#endif > +#include > +#include > +#include > +#include > + > +#include > +#include > +#include > + > +static int > +firmware_read(const char *name, void **buf, size_t *bufsz) > +{ > + const size_t blocksize = 4096; > + int ret = -1; > + int err; > +#ifdef RTE_HAS_LIBARCHIVE I think, better to have small inline functions for libarchive variant vs normal file accessors in the group for open, access, read etc to avoid the ifdef clutter and manage with one ifdef. > + struct archive_entry *entry; > + struct archive *a; > +#else > + int fd; > +#endif > + > + *buf = NULL; > + *bufsz = 0; > + > +#ifdef RTE_HAS_LIBARCHIVE See above > + a = archive_read_new(); > + if (a == NULL || archive_read_support_format_raw(a) != ARCHIVE_OK || > + archive_read_support_filter_xz(a) != ARCHIVE_OK || > + archive_read_open_filename(a, name, blocksize) != ARCHIVE_OK || > + archive_read_next_header(a, &entry) != ARCHIVE_OK) > + goto out; > +#else > + fd = open(name, O_RDONLY); > + if (fd < 0) > + goto out; > +#endif > + > + do { > + void *tmp; > + > + tmp = realloc(*buf, *bufsz + blocksize); > + if (tmp == NULL) { > + free(*buf); > + *buf = NULL; > + *bufsz = 0; > + break; > + } > + *buf = tmp; > + > +#ifdef RTE_HAS_LIBARCHIVE See above > + err = archive_read_data(a, RTE_PTR_ADD(*buf, *bufsz), blocksize); > +#else > + err = read(fd, RTE_PTR_ADD(*buf, *bufsz), blocksize); > +#endif > + if (err < 0) { > + free(*buf); > + *buf = NULL; > + *bufsz = 0; > + break; > + } > + *bufsz += err; > + > + } while (err != 0); > + > + if (*buf != NULL) > + ret = 0; > +out: > +#ifdef RTE_HAS_LIBARCHIVE See above > + if (a != NULL) > + archive_read_free(a); > +#else > + if (fd >= 0) > + close(fd); > +#endif > + return ret; > +} > + > +int > +rte_firmware_read(const char *name, void **buf, size_t *bufsz) > +{ > + char path[PATH_MAX]; > + int ret; > + > + ret = firmware_read(name, buf, bufsz); > + if (ret < 0) { > + snprintf(path, sizeof(path), "%s.xz", name); > + path[PATH_MAX - 1] = '\0'; > +#ifndef RTE_HAS_LIBARCHIVE See above > + if (access(path, F_OK) == 0) { > + RTE_LOG(WARNING, EAL, "libarchive not available, %s cannot be decompressed\n", > + path); > + } > +#else > + ret = firmware_read(path, buf, bufsz); > +#endif >