From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.3 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_2 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BC393C4338F for ; Thu, 19 Aug 2021 10:28:29 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id F27AC61139 for ; Thu, 19 Aug 2021 10:28:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org F27AC61139 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=nic.cz Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 24F4782DD8; Thu, 19 Aug 2021 12:28:27 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=nic.cz Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; secure) header.d=nic.cz header.i=@nic.cz header.b="ikq1jb9H"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id D207D82DB2; Thu, 19 Aug 2021 12:28:25 +0200 (CEST) Received: from mail.nic.cz (mail.nic.cz [IPv6:2001:1488:800:400::400]) (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 89E9D82DB2 for ; Thu, 19 Aug 2021 12:28:21 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=nic.cz Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=marek.behun@nic.cz Received: from thinkpad (unknown [172.20.6.87]) by mail.nic.cz (Postfix) with ESMTPSA id 2F051140A8D; Thu, 19 Aug 2021 12:28:21 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=nic.cz; s=default; t=1629368901; bh=Icu5Tv4hOE7bJ+JKSU5+uugo87OD3k85Ba9uxRIwdLY=; h=Date:From:To; b=ikq1jb9HIL2fdE7zn6vaxYclgOS2VQh79nIds4D5I9GlD+TWpLES90KF/axA4NwOL 116wNPgMa3Fd4pp0968DzVA4iobkP22RCjA+ZI1sEBgPXasPyHR7a8mf64EYR35olJ Ha/Nnzdgo1GGz91eEi0yCgN/IibA197/VbXwabzI= Date: Thu, 19 Aug 2021 12:28:20 +0200 From: Marek =?UTF-8?B?QmVow7pu?= To: Simon Glass Cc: U-Boot Mailing List , Heinrich Schuchardt , Patrick Delaunay Subject: Re: [PATCH 09/11] sandbox: Add a way to map a file into memory Message-ID: <20210819122820.67b413a9@thinkpad> In-Reply-To: <20210818214022.9.Ifb64a64e67b3777340ca034ed6d3a7a02143696c@changeid> References: <20210819034033.1617201-1-sjg@chromium.org> <20210818214022.9.Ifb64a64e67b3777340ca034ed6d3a7a02143696c@changeid> X-Mailer: Claws Mail 3.18.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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.2 at phobos.denx.de X-Virus-Status: Clean On Wed, 18 Aug 2021 21:40:31 -0600 Simon Glass wrote: > It is useful to map a file into memory so that it can be accessed using > simple pointers. Add a function to support this. > > Signed-off-by: Simon Glass > +int os_map_file(const char *pathname, int os_flags, void **bufp, int *sizep) > +{ > + void *ptr; > + int size; > + int ifd; > + > + ifd = os_open(pathname, os_flags); > + if (ifd < 0) { > + printf("Cannot open file '%s'\n", pathname); > + return -EIO; > + } > + size = os_filesize(ifd); > + if (size < 0) { > + printf("Cannot get file size of '%s'\n", pathname); > + return -EIO; > + } > + > + ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0); > + if (ptr == MAP_FAILED) { > + printf("Can't map file '%s': %s\n", pathname, strerror(errno)); > + return -EPERM; > + } > + > + *bufp = ptr; > + *sizep = size; > + > + return 0; > +} You need to close the file descriptor after mmapping. Marek