summaryrefslogtreecommitdiff
path: root/com32/lib/sys
diff options
context:
space:
mode:
authorhpa <hpa>2004-11-10 22:31:50 +0000
committerhpa <hpa>2004-11-10 22:31:50 +0000
commit17f967640cef484f83d755c9dd016a946711236f (patch)
tree00bf0c0f2926cd6a9761eb372b55d090305aca44 /com32/lib/sys
parentc67a2ac96611fa6aeb9ff3602c5e0c8265f1cc9d (diff)
downloadsyslinux-17f967640cef484f83d755c9dd016a946711236f.tar.gz
Very first cut at a klibc-derived C library for com32
Diffstat (limited to 'com32/lib/sys')
-rw-r--r--com32/lib/sys/close.c57
-rw-r--r--com32/lib/sys/entry.S83
-rw-r--r--com32/lib/sys/exit.S27
-rw-r--r--com32/lib/sys/file.h69
-rw-r--r--com32/lib/sys/fileinfo.c3
-rw-r--r--com32/lib/sys/open.c79
-rw-r--r--com32/lib/sys/read.c92
-rw-r--r--com32/lib/sys/write.c58
8 files changed, 468 insertions, 0 deletions
diff --git a/com32/lib/sys/close.c b/com32/lib/sys/close.c
new file mode 100644
index 00000000..b81d77ef
--- /dev/null
+++ b/com32/lib/sys/close.c
@@ -0,0 +1,57 @@
+#ident "$Id$"
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 2004 H. Peter Anvin - All Rights Reserved
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall
+ * be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * close.c
+ */
+
+#include <errno.h>
+#include <com32.h>
+#include <string.h>
+#include "file.h"
+
+int close(int fd)
+{
+ com32sys_t regs;
+ struct file_info *fp = &__file_info[fd];
+
+ if ( fd >= NFILES || fp->blocklg2 == 0 ) {
+ errno = EBADF;
+ return -1;
+ }
+
+ if ( fp->filedes ) {
+ memset(&regs, 0, sizeof regs);
+ regs.eax.w[0] = 0x0008; /* Close file */
+ regs.esi.w[0] = fp->filedes;
+
+ __com32.cs_intcall(0x22, &regs, NULL);
+ }
+
+ return 0;
+}
diff --git a/com32/lib/sys/entry.S b/com32/lib/sys/entry.S
new file mode 100644
index 00000000..40194528
--- /dev/null
+++ b/com32/lib/sys/entry.S
@@ -0,0 +1,83 @@
+#ident "$Id$"
+# -----------------------------------------------------------------------
+#
+# Copyright 2003-2004 H. Peter Anvin - All Rights Reserved
+#
+# Permission is hereby granted, free of charge, to any person
+# obtaining a copy of this software and associated documentation
+# files (the "Software"), to deal in the Software without
+# restriction, including without limitation the rights to use,
+# copy, modify, merge, publish, distribute, sublicense, and/or
+# sell copies of the Software, and to permit persons to whom
+# the Software is furnished to do so, subject to the following
+# conditions:
+#
+# The above copyright notice and this permission notice shall
+# be included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+# OTHER DEALINGS IN THE SOFTWARE.
+#
+# -----------------------------------------------------------------------
+
+# COM32 start up code - must be linked first in the binary
+
+
+ .section ".init","ax"
+ .globl _start
+ .type _start, @function
+_start:
+ # This first instruction acts as COM32 magic number
+ movl $0x21cd4cff,%eax
+
+ # Upwards string operations
+ cld
+
+ # Zero the .bss segment
+ xorl %eax,%eax
+ movl $__bss_start,%edi # Symbol provided by linker
+ movl $_end+3,%ecx # Symbol provided by linker
+ subl %edi,%ecx
+ shrl $2,%ecx
+ rep ; stosl
+
+ # Copy COM32 invocation parameters
+ leal 4(%esp),%esi # Argument list
+ movl $__com32,%edi
+ movl $6,%ecx
+ movl %esp,-4(%edi) # Save the initial stack ptr
+ cmpl (%esi),%ecx
+ jbe 1f
+ movl (%esi),%ecx
+1: rep ; movsl
+
+ # Look for library initialization functions
+ movl $__ctors_start, %esi
+2:
+ cmpl $__ctors_end, %esi
+ jae 3f
+ call *(%esi)
+ addl $4,%esi
+ jmp 2b
+
+#
+# Run program. Note that we dont actually pass argc, argv to main...
+#
+3:
+ call main
+ pushl %eax
+ call *(__exit_handler)
+ hlt
+ .size _start, .-_start
+
+ .bss
+ .globl __entry_esp
+__entry_esp: .space 4
+ .globl __com32
+__com32: .space 4*6
diff --git a/com32/lib/sys/exit.S b/com32/lib/sys/exit.S
new file mode 100644
index 00000000..76c8b5da
--- /dev/null
+++ b/com32/lib/sys/exit.S
@@ -0,0 +1,27 @@
+# $Id$
+#
+# Implementation of _exit() for com32 based on c32entry.S
+#
+ .text
+ .globl _exit
+ .type _exit, @function
+_exit:
+ # Run any destructors
+ movl $__dtors_start, %esi
+2:
+ cmpl $__dtors_end, %esi
+ jae 1f
+ call *(%esi)
+ addl $4,%esi
+ jmp 2b
+
+1:
+ movl 4(%esp),%eax # Exit code in %eax = return value
+ movl (__entry_esp),%esp # Return stack pointer to entry value
+ ret # Return to termination address
+ .size _exit, .-_exit
+
+ .data
+__exit_handler:
+ .globl __exit_handler
+ .long _exit
diff --git a/com32/lib/sys/file.h b/com32/lib/sys/file.h
new file mode 100644
index 00000000..6a56f587
--- /dev/null
+++ b/com32/lib/sys/file.h
@@ -0,0 +1,69 @@
+#ident "$Id$"
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 2003 H. Peter Anvin - All Rights Reserved
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall
+ * be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * file.h
+ *
+ * Internal implementation of file I/O for COM32
+ */
+
+#ifndef _COM32_SYS_FILE_H
+#define _COM32_SYS_FILE_H
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <sys/types.h>
+
+/* Ordinary file */
+
+#define NFILES 32 /* Number of files to support */
+#define MAXBLOCK 16384 /* Defined by ABI */
+
+struct file_info {
+ int blocklg2; /* Blocksize log 2 */
+ size_t offset; /* Current file offset */
+ size_t length; /* Total file length */
+ uint16_t filedes; /* File descriptor */
+ uint16_t _filler; /* Unused */
+ size_t nbytes; /* Number of bytes available in buffer */
+ char *datap; /* Current data pointer */
+ char buf[MAXBLOCK];
+};
+
+extern struct file_info __file_info[NFILES];
+
+/* Special device (tty et al) */
+
+#define __DEV_MAGIC 0x504af4e7
+struct dev_info {
+ uint32_t dev_magic; /* Magic number */
+ ssize_t (*read)(int, void *, size_t);
+ ssize_t (*write)(int, const void *, size_t);
+};
+
+#endif /* _COM32_SYS_FILE_H */
diff --git a/com32/lib/sys/fileinfo.c b/com32/lib/sys/fileinfo.c
new file mode 100644
index 00000000..a1fc7c94
--- /dev/null
+++ b/com32/lib/sys/fileinfo.c
@@ -0,0 +1,3 @@
+#include "file.h"
+
+struct file_info __file_info[NFILES];
diff --git a/com32/lib/sys/open.c b/com32/lib/sys/open.c
new file mode 100644
index 00000000..3f84e5f7
--- /dev/null
+++ b/com32/lib/sys/open.c
@@ -0,0 +1,79 @@
+#ident "$Id$"
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 2003 H. Peter Anvin - All Rights Reserved
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall
+ * be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * ----------------------------------------------------------------------- */
+
+#include <errno.h>
+#include <com32.h>
+#include <string.h>
+#include "file.h"
+
+int open(const char *pathname, int flags, mode_t mode)
+{
+ com32sys_t regs;
+ int fd;
+ struct file_info *fp;
+
+ if ( flags ) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ for ( fd = 0, fp = __file_info ; fd < NFILES ; fd++, fp++ )
+ if ( fp->blocklg2 == 0 )
+ break;
+
+ if ( fd >= NFILES ) {
+ errno = EMFILE;
+ return -1;
+ }
+
+ strlcpy(__com32.cs_bounce, pathname, __com32.cs_bounce_size);
+
+ regs.eax.w[0] = 0x0006;
+ regs.esi.w[0] = OFFS(__com32.cs_bounce);
+ regs.es = SEG(__com32.cs_bounce);
+
+ __com32.cs_intcall(0x22, &regs, &regs);
+
+ if ( (regs.eflags.l & EFLAGS_CF) || regs.esi.w[0] == 0 ) {
+ errno = ENOENT;
+ return -1;
+ }
+
+ {
+ uint16_t blklg2;
+ asm("bsrw %1,%0" : "=r" (blklg2) : "rm" (regs.ecx.w[0]));
+ fp->blocklg2 = blklg2;
+ }
+ fp->length = regs.eax.l;
+ fp->filedes = regs.esi.w[0];
+ fp->offset = 0;
+ fp->nbytes = 0;
+ fp->datap = fp->buf;
+
+ return fd;
+}
diff --git a/com32/lib/sys/read.c b/com32/lib/sys/read.c
new file mode 100644
index 00000000..1c16ce47
--- /dev/null
+++ b/com32/lib/sys/read.c
@@ -0,0 +1,92 @@
+#ident "$Id$"
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 2004 H. Peter Anvin - All Rights Reserved
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall
+ * be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * read.c
+ *
+ * Reading from a file
+ */
+
+#include <errno.h>
+#include <string.h>
+#include <com32.h>
+#include <minmax.h>
+#include "file.h"
+
+ssize_t read(int fd, void *buf, size_t count)
+{
+ com32sys_t ireg, oreg;
+ struct file_info *fp = &__file_info[fd];
+ char *bufp = buf;
+ size_t n = 0;
+ size_t ncopy;
+
+ if ( fd >= NFILES || fp->blocklg2 == 0 ) {
+ errno = EBADF;
+ return -1;
+ }
+
+ memset(&ireg, 0, sizeof ireg);
+ ireg.eax.w[0] = 0x0007; /* Read file */
+ ireg.esi.w[0] = OFFS(__com32.cs_bounce);
+ ireg.es = SEG(__com32.cs_bounce);
+
+ while ( count ) {
+ if ( fp->nbytes == 0 ) {
+ if ( fp->offset >= fp->length || !fp->filedes )
+ return n; /* As good as it gets... */
+
+ ireg.esi.w[0] = fp->filedes;
+ ireg.ecx.w[0] = MAXBLOCK >> fp->blocklg2;
+
+ __intcall(0x22, &ireg, &oreg);
+
+ if ( oreg.eflags.l & EFLAGS_CF ) {
+ errno = EIO;
+ return -1;
+ }
+
+ fp->filedes = ireg.esi.w[0];
+ fp->nbytes = min(fp->length-fp->offset, (unsigned)MAXBLOCK);
+ fp->datap = fp->buf;
+ memcpy(fp->buf, __com32.cs_bounce, fp->nbytes);
+ }
+
+ ncopy = min(count, fp->nbytes);
+ memcpy(bufp, fp->datap, ncopy);
+
+ n += ncopy;
+ bufp += ncopy;
+ count -= ncopy;
+ fp->datap += ncopy;
+ fp->offset += ncopy;
+ fp->nbytes -= ncopy;
+ }
+
+ return n;
+}
diff --git a/com32/lib/sys/write.c b/com32/lib/sys/write.c
new file mode 100644
index 00000000..b908f362
--- /dev/null
+++ b/com32/lib/sys/write.c
@@ -0,0 +1,58 @@
+#ident "$Id$"
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 2004 H. Peter Anvin - All Rights Reserved
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall
+ * be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * write.c
+ *
+ * Writing to the console
+ */
+
+#include <errno.h>
+#include <string.h>
+#include <com32.h>
+#include <minmax.h>
+#include "file.h"
+
+ssize_t write(int fd, void *buf, size_t count)
+{
+ com32sys_t ireg;
+ struct file_info *fp = &__file_info[fd];
+ char *bufp = buf;
+ size_t n = 0;
+
+ memset(&ireg, 0, sizeof ireg);
+ ireg.eax.b[1] = 0x02;
+
+ while ( count-- ) {
+ ireg.edx.b[0] = *bufp++;
+ __intcall(0x21, &ireg, NULL);
+ n++;
+ }
+
+ return n;
+}