public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* EmulatorPkg: fixes for NetBSD compilation
@ 2022-11-21 13:08 tlaronde
  2022-11-21 22:32 ` [edk2-devel] " Pedro Falcato
  0 siblings, 1 reply; 3+ messages in thread
From: tlaronde @ 2022-11-21 13:08 UTC (permalink / raw)
  To: devel

diff --git a/EmulatorPkg/Unix/Host/BlockIo.c b/EmulatorPkg/Unix/Host/BlockIo.c
index cf2d6b4cda..c0c694be55 100644
--- a/EmulatorPkg/Unix/Host/BlockIo.c
+++ b/EmulatorPkg/Unix/Host/BlockIo.c
@@ -133,6 +133,20 @@ EmuBlockIoOpenDevice (
 
       ioctl (Private->fd, DKIOCGETMAXBLOCKCOUNTWRITE, &Private->Media->OptimalTransferLengthGranularity);
     }
+ #elif _NETBSD_SOURCE
+    {
+      u_int  BlockSize;
+      off_t  DiskSize;
+
+      if (ioctl (Private->fd,  DIOCGSECTORSIZE, &BlockSize) == 0) {
+        Private->Media->BlockSize = BlockSize;
+      }
+
+      if (ioctl (Private->fd, DIOCGMEDIASIZE, &DiskSize) == 0) {
+        Private->NumberOfBlocks   = DivU64x32 (DiskSize, (UINT32)BlockSize);
+        Private->Media->LastBlock = Private->NumberOfBlocks - 1;
+      }
+    }
  #else
     {
       size_t  BlockSize;
@@ -154,7 +168,7 @@ EmuBlockIoOpenDevice (
     Private->Media->LastBlock = Private->NumberOfBlocks - 1;
 
     if (fstatfs (Private->fd, &buf) == 0) {
- #if __APPLE__
+ #if __APPLE__ || _NETBSD_SOURCE
       Private->Media->OptimalTransferLengthGranularity = buf.f_iosize/buf.f_bsize;
  #else
       Private->Media->OptimalTransferLengthGranularity = buf.f_bsize/buf.f_bsize;
diff --git a/EmulatorPkg/Unix/Host/Host.c b/EmulatorPkg/Unix/Host/Host.c
index 38c01c84af..c505300129 100644
--- a/EmulatorPkg/Unix/Host/Host.c
+++ b/EmulatorPkg/Unix/Host/Host.c
@@ -12,6 +12,12 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #define MAP_ANONYMOUS  MAP_ANON
 #endif
 
+#ifdef _NETBSD_SOURCE
+#define MAP_ANON_FD_ (-1)
+#else
+#define MAP_ANON_FD_ (0)
+#endif
+
 //
 // Globals
 //
@@ -187,7 +193,7 @@ main (
   //
   InitialStackMemorySize = STACK_SIZE;
   InitialStackMemory     = (UINTN)MapMemory (
-                                    0,
+                                    MAP_ANON_FD_,
                                     (UINT32)InitialStackMemorySize,
                                     PROT_READ | PROT_WRITE | PROT_EXEC,
                                     MAP_ANONYMOUS | MAP_PRIVATE
@@ -348,6 +354,7 @@ MapMemory (
   while ((!isAligned) && (base != 0)) {
     res = mmap ((void *)base, length, prot, flags, fd, 0);
     if (res == MAP_FAILED) {
+      perror("MapMemory");
       return NULL;
     }
 
@@ -640,7 +647,7 @@ SecUnixPeiAutoScan (
 
   *MemoryBase = 0;
   res         = MapMemory (
-                  0,
+                  MAP_ANON_FD_,
                   gSystemMemory[Index].Size,
                   PROT_READ | PROT_WRITE | PROT_EXEC,
                   MAP_PRIVATE | MAP_ANONYMOUS
diff --git a/EmulatorPkg/Unix/Host/Host.h b/EmulatorPkg/Unix/Host/Host.h
index 0c81cdfc01..0de925adaf 100644
--- a/EmulatorPkg/Unix/Host/Host.h
+++ b/EmulatorPkg/Unix/Host/Host.h
@@ -31,6 +31,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 
 #if __CYGWIN__
   #include <sys/dirent.h>
+#elif _NETBSD_SOURCE
+  #include <dirent.h>
 #else
   #include <sys/dir.h>
 #endif
@@ -55,7 +57,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include <net/if.h>
 #include <ifaddrs.h>
 
-#ifdef __APPLE__
+#if defined(__APPLE__)
   #include <net/if_dl.h>
   #include <net/bpf.h>
   #include <sys/param.h>
@@ -65,6 +67,9 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
   #ifndef _Bool
 #define _Bool  char  // for clang debug
   #endif
+#elif defined(_NETBSD_SOURCE)
+  #define statfs statvfs
+  #define fstatfs fstatvfs
 #else
   #include <termio.h>
   #include <sys/vfs.h>

signed-off-by: Thierry LARONDE <tlaronde@polynum.com>
-- 
        Thierry Laronde <tlaronde +AT+ polynum +dot+ com>
                     http://www.kergis.com/
                    http://kertex.kergis.com/
Key fingerprint = 0FF7 E906 FBAF FE95 FD89  250D 52B1 AE95 6006 F40C

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [edk2-devel] EmulatorPkg: fixes for NetBSD compilation
  2022-11-21 13:08 EmulatorPkg: fixes for NetBSD compilation tlaronde
@ 2022-11-21 22:32 ` Pedro Falcato
  2022-11-22  8:35   ` tlaronde
  0 siblings, 1 reply; 3+ messages in thread
From: Pedro Falcato @ 2022-11-21 22:32 UTC (permalink / raw)
  To: devel, tlaronde

[-- Attachment #1: Type: text/plain, Size: 2098 bytes --]

On Mon, Nov 21, 2022 at 9:21 PM <tlaronde@polynum.com> wrote:

> diff --git a/EmulatorPkg/Unix/Host/BlockIo.c
> b/EmulatorPkg/Unix/Host/BlockIo.c
> index cf2d6b4cda..c0c694be55 100644
> --- a/EmulatorPkg/Unix/Host/BlockIo.c
> +++ b/EmulatorPkg/Unix/Host/BlockIo.c
> @@ -133,6 +133,20 @@ EmuBlockIoOpenDevice (
>
>        ioctl (Private->fd, DKIOCGETMAXBLOCKCOUNTWRITE,
> &Private->Media->OptimalTransferLengthGranularity);
>      }
> + #elif _NETBSD_SOURCE
> +    {
> +      u_int  BlockSize;
>
Hi,

Again, thanks for the patches. Please send them in the way I kind of
described in my other reply.

s/u_int/UINT/

+      off_t  DiskSize;
>
I think this off_t is fine, per the other off_t usages, I don't know if the
maintainers agree.

> +
> +      if (ioctl (Private->fd,  DIOCGSECTORSIZE, &BlockSize) == 0) {
> +        Private->Media->BlockSize = BlockSize;
> +      }
> +
> +      if (ioctl (Private->fd, DIOCGMEDIASIZE, &DiskSize) == 0) {
> +        Private->NumberOfBlocks   = DivU64x32 (DiskSize,
> (UINT32)BlockSize);
> +        Private->Media->LastBlock = Private->NumberOfBlocks - 1;
> +      }
> +    }
>   #else
>      {
>        size_t  BlockSize;
>


> diff --git a/EmulatorPkg/Unix/Host/Host.c b/EmulatorPkg/Unix/Host/Host.c
> index 38c01c84af..c505300129 100644
> --- a/EmulatorPkg/Unix/Host/Host.c
> +++ b/EmulatorPkg/Unix/Host/Host.c
> @@ -12,6 +12,12 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>  #define MAP_ANONYMOUS  MAP_ANON
>  #endif
>
> +#ifdef _NETBSD_SOURCE
> +#define MAP_ANON_FD_ (-1)
> +#else
> +#define MAP_ANON_FD_ (0)
> +#endif
>
Would there be a harm if we just passed -1 everywhere? It's a bit odd
NetBSD explicitly requires this, but AFAIK implementations
either EINVAL on fd != -1 or take whatever since it's anon. The main
implementations (Linux, FreeBSD, NetBSD, OpenBSD, macOS) seem to
agree that passing -1 should be safe everywhere (in fact, it's pretty funny
that all BSD-derived implementations agree with the fd = -1 thing, but I
digress).

The rest of the patch looks mostly ok to me, but then again, I'm not a
maintainer for this.

Thanks,
Pedro

[-- Attachment #2: Type: text/html, Size: 3175 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [edk2-devel] EmulatorPkg: fixes for NetBSD compilation
  2022-11-21 22:32 ` [edk2-devel] " Pedro Falcato
@ 2022-11-22  8:35   ` tlaronde
  0 siblings, 0 replies; 3+ messages in thread
From: tlaronde @ 2022-11-22  8:35 UTC (permalink / raw)
  To: Pedro Falcato; +Cc: devel

Hello Pedro,

Le Mon, Nov 21, 2022 at 10:32:51PM +0000, Pedro Falcato a écrit :
> On Mon, Nov 21, 2022 at 9:21 PM <tlaronde@polynum.com> wrote:
> 
> > diff --git a/EmulatorPkg/Unix/Host/BlockIo.c
> > b/EmulatorPkg/Unix/Host/BlockIo.c
> > index cf2d6b4cda..c0c694be55 100644
> > --- a/EmulatorPkg/Unix/Host/BlockIo.c
> > +++ b/EmulatorPkg/Unix/Host/BlockIo.c
> > @@ -133,6 +133,20 @@ EmuBlockIoOpenDevice (
> >
> >        ioctl (Private->fd, DKIOCGETMAXBLOCKCOUNTWRITE,
> > &Private->Media->OptimalTransferLengthGranularity);
> >      }
> > + #elif _NETBSD_SOURCE
> > +    {
> > +      u_int  BlockSize;
> >
> Hi,
> 
> Again, thanks for the patches. Please send them in the way I kind of
> described in my other reply.
> 
> s/u_int/UINT/
> 
> +      off_t  DiskSize;
> >
> I think this off_t is fine, per the other off_t usages, I don't know if the
> maintainers agree.
> 
> > +
> > +      if (ioctl (Private->fd,  DIOCGSECTORSIZE, &BlockSize) == 0) {
> > +        Private->Media->BlockSize = BlockSize;
> > +      }
> > +
> > +      if (ioctl (Private->fd, DIOCGMEDIASIZE, &DiskSize) == 0) {
> > +        Private->NumberOfBlocks   = DivU64x32 (DiskSize,
> > (UINT32)BlockSize);
> > +        Private->Media->LastBlock = Private->NumberOfBlocks - 1;
> > +      }
> > +    }
> >   #else
> >      {
> >        size_t  BlockSize;
> >
> 
> 
> > diff --git a/EmulatorPkg/Unix/Host/Host.c b/EmulatorPkg/Unix/Host/Host.c
> > index 38c01c84af..c505300129 100644
> > --- a/EmulatorPkg/Unix/Host/Host.c
> > +++ b/EmulatorPkg/Unix/Host/Host.c
> > @@ -12,6 +12,12 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> >  #define MAP_ANONYMOUS  MAP_ANON
> >  #endif
> >
> > +#ifdef _NETBSD_SOURCE
> > +#define MAP_ANON_FD_ (-1)
> > +#else
> > +#define MAP_ANON_FD_ (0)
> > +#endif
> >
> Would there be a harm if we just passed -1 everywhere? It's a bit odd
> NetBSD explicitly requires this, but AFAIK implementations
> either EINVAL on fd != -1 or take whatever since it's anon. The main
> implementations (Linux, FreeBSD, NetBSD, OpenBSD, macOS) seem to
> agree that passing -1 should be safe everywhere (in fact, it's pretty funny
> that all BSD-derived implementations agree with the fd = -1 thing, but I
> digress).

Since MAP_ANON is not specified by POSIX1, I tend to think that what is
not in POSIX should be explicitely set in the code.

So if other OSes (with which edk2 is used) agree on (-1), my personal
taste would be to keep at least the define macro:

#define MAP_ANON_FD_ (-1)

as an indication that the definition is not standard (so that someone
with a system choking on this, could easily spot the culprit). (The
trailing underscore is because leading ones shall be reserved for
standard or system implementation, and the trailing is a way to indicate
that the macro is somehow local. Is there a convention for this in 
EDK2?)

Best,
-- 
        Thierry Laronde <tlaronde +AT+ polynum +dot+ com>
                     http://www.kergis.com/
                    http://kertex.kergis.com/
Key fingerprint = 0FF7 E906 FBAF FE95 FD89  250D 52B1 AE95 6006 F40C

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-11-22  8:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-21 13:08 EmulatorPkg: fixes for NetBSD compilation tlaronde
2022-11-21 22:32 ` [edk2-devel] " Pedro Falcato
2022-11-22  8:35   ` tlaronde

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox