libupload: New library to manage uploads
authorErwan Velu <erwanaliasr1@gmail.com>
Tue, 15 Mar 2011 20:51:46 +0000 (21:51 +0100)
committerErwan Velu <erwanaliasr1@gmail.com>
Tue, 15 Mar 2011 20:58:47 +0000 (21:58 +0100)
This commit creates a library to upload content via 3 backends
(srec/ymodem/tftp).

Code came from sysdump and got librarized for being used more easily
by more other com32 modules.

26 files changed:
com32/Makefile
com32/libupload/.gitignore [new file with mode: 0644]
com32/libupload/Makefile [new file with mode: 0644]
com32/libupload/cpio.c [moved from com32/sysdump/cpio.c with 80% similarity]
com32/libupload/ctime.c [moved from com32/sysdump/ctime.c with 100% similarity]
com32/libupload/ctime.h [moved from com32/sysdump/ctime.h with 100% similarity]
com32/libupload/serial.c [moved from com32/sysdump/serial.c with 100% similarity]
com32/libupload/serial.h [moved from com32/sysdump/serial.h with 100% similarity]
com32/libupload/srecsend.h [moved from com32/sysdump/srecsend.h with 100% similarity]
com32/libupload/upload_backend.h [new file with mode: 0644]
com32/libupload/upload_srec.c [moved from com32/sysdump/be_srec.c with 91% similarity]
com32/libupload/upload_tftp.c [moved from com32/sysdump/be_tftp.c with 96% similarity]
com32/libupload/upload_ymodem.c [moved from com32/sysdump/be_ymodem.c with 96% similarity]
com32/libupload/ymodem.txt [moved from com32/sysdump/ymodem.txt with 100% similarity]
com32/libupload/zout.c [moved from com32/sysdump/zout.c with 87% similarity]
com32/sysdump/Makefile
com32/sysdump/acpi.c
com32/sysdump/backend.h [deleted file]
com32/sysdump/cpuid.c
com32/sysdump/dmi.c
com32/sysdump/main.c
com32/sysdump/memmap.c
com32/sysdump/memory.c
com32/sysdump/pci.c
com32/sysdump/sysdump.h
com32/sysdump/vesa.c

index b090c40..da632a1 100644 (file)
@@ -1,4 +1,4 @@
-SUBDIRS = tools lib gpllib libutil modules mboot menu samples rosh cmenu \
+SUBDIRS = libupload tools lib gpllib libutil modules mboot menu samples rosh cmenu \
          hdt gfxboot sysdump lua/src
 
 all tidy dist clean spotless install:
diff --git a/com32/libupload/.gitignore b/com32/libupload/.gitignore
new file mode 100644 (file)
index 0000000..e0292b1
--- /dev/null
@@ -0,0 +1,2 @@
+*.o
+*.a
diff --git a/com32/libupload/Makefile b/com32/libupload/Makefile
new file mode 100644 (file)
index 0000000..c009b6b
--- /dev/null
@@ -0,0 +1,38 @@
+# Include configuration rules
+topdir = ../..
+include ../lib/MCONFIG
+
+REQFLAGS += -I./
+
+SUBDIRS := . 
+LIBOBJS := $(foreach dir,$(SUBDIRS),$(patsubst %.c,%.o,$(wildcard $(dir)/*.c)))
+
+BINDIR   = /usr/bin
+LIBDIR   = /usr/lib
+DATADIR  = /usr/share
+AUXDIR   = $(DATADIR)/syslinux
+INCDIR   = /usr/include
+COM32DIR = $(AUXDIR)/com32
+
+all: libcom32upload.a
+
+libcom32upload.a : $(LIBOBJS)
+       rm -f $@
+       $(AR) cq $@ $^
+       $(RANLIB) $@
+
+tidy dist clean:
+       find . \( -name \*.o -o -name \*.a -o -name .\*.d -o -name \*.tmp \) -print0 | \
+               xargs -0r rm -f
+
+spotless: clean
+       rm -f *.a
+       rm -f *~ \#* */*~ */\#*
+
+install: all
+       mkdir -m 755 -p $(INSTALLROOT)$(COM32DIR)
+       install -m 644 libcom32upload.a $(INSTALLROOT)$(COM32DIR)
+       mkdir -p $(INSTALLROOT)$(COM32DIR)/include/
+       cp -r *.h $(INSTALLROOT)$(COM32DIR)/include/
+
+-include .*.d */.*.d */*/.*.d
similarity index 80%
rename from com32/sysdump/cpio.c
rename to com32/libupload/cpio.c
index 81d0d4b..b3e1eb7 100644 (file)
@@ -9,10 +9,10 @@
 #include <inttypes.h>
 #include <stdbool.h>
 #include <zlib.h>
-#include "backend.h"
+#include "upload_backend.h"
 #include "ctime.h"
 
-int cpio_pad(struct backend *be)
+int cpio_pad(struct upload_backend *be)
 {
     static char pad[4];                /* Up to 4 zero bytes */
     if (be->dbytes & 3)
@@ -21,7 +21,7 @@ int cpio_pad(struct backend *be)
        return 0;
 }
 
-int cpio_hdr(struct backend *be, uint32_t mode, size_t datalen,
+int cpio_hdr(struct upload_backend *be, uint32_t mode, size_t datalen,
             const char *filename)
 {
     static uint32_t inode = 2;
@@ -52,12 +52,12 @@ int cpio_hdr(struct backend *be, uint32_t mode, size_t datalen,
     return rv;
 }
 
-int cpio_mkdir(struct backend *be, const char *filename)
+int cpio_mkdir(struct upload_backend *be, const char *filename)
 {
     return cpio_hdr(be, MODE_DIR, 0, filename);
 }
 
-int cpio_writefile(struct backend *be, const char *filename,
+int cpio_writefile(struct upload_backend *be, const char *filename,
                   const void *data, size_t len)
 {
     int rv;
@@ -69,7 +69,7 @@ int cpio_writefile(struct backend *be, const char *filename,
     return rv;
 }
 
-int cpio_close(struct backend *be)
+int cpio_close(struct upload_backend *be)
 {
     return cpio_hdr(be, 0, 0, "TRAILER!!!");
 }
diff --git a/com32/libupload/upload_backend.h b/com32/libupload/upload_backend.h
new file mode 100644 (file)
index 0000000..968e2c6
--- /dev/null
@@ -0,0 +1,55 @@
+#ifndef BACKEND_H
+#define BACKEND_H
+
+#include <stddef.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <zlib.h>
+#include "serial.h"
+
+/* Backend flags */
+#define BE_NEEDLEN     0x01
+
+struct upload_backend {
+    const char *name;
+    const char *helpmsg;
+    int minargs;
+
+    size_t dbytes;
+    size_t zbytes;
+    const char **argv;
+
+    uint32_t now;
+
+    int (*write)(struct upload_backend *);
+
+    z_stream zstream;
+    char *outbuf;
+    size_t alloc;
+};
+
+/* zout.c */
+int init_data(struct upload_backend *be, const char *argv[]);
+int write_data(struct upload_backend *be, const void *buf, size_t len);
+int flush_data(struct upload_backend *be);
+
+/* cpio.c */
+#define cpio_init init_data
+int cpio_hdr(struct upload_backend *be, uint32_t mode, size_t datalen,
+            const char *filename);
+int cpio_mkdir(struct upload_backend *be, const char *filename);
+int cpio_writefile(struct upload_backend *be, const char *filename,
+                  const void *data, size_t len);
+int cpio_close(struct upload_backend *be);
+#define MODE_FILE      0100644
+#define MODE_DIR       0040755
+
+/* backends.c */
+struct upload_backend *get_upload_backend(const char *name);
+
+/* backends */
+extern struct upload_backend upload_tftp;
+extern struct upload_backend upload_ymodem;
+extern struct upload_backend upload_srec;
+
+#endif /* BACKEND_H */
similarity index 91%
rename from com32/sysdump/be_srec.c
rename to com32/libupload/upload_srec.c
index fc69c88..c190713 100644 (file)
@@ -6,7 +6,7 @@
 #include <stdio.h>
 #include <inttypes.h>
 #include <minmax.h>
-#include "backend.h"
+#include "upload_backend.h"
 
 /* Write a single S-record */
 static int write_srecord(unsigned int len,  unsigned int alen,
@@ -43,7 +43,7 @@ static int write_srecord(unsigned int len,  unsigned int alen,
     return 0;
 }
 
-static int be_srec_write(struct backend *be)
+static int upload_srec_write(struct upload_backend *be)
 {
     char name[33];
     const char *buf;
@@ -77,9 +77,9 @@ static int be_srec_write(struct backend *be)
     return 0;
 }
 
-struct backend be_srec = {
+struct upload_backend upload_srec = {
     .name       = "srec",
     .helpmsg    = "[filename]",
     .minargs    = 0,
-    .write      = be_srec_write,
+    .write      = upload_srec_write,
 };
similarity index 96%
rename from com32/sysdump/be_tftp.c
rename to com32/libupload/upload_tftp.c
index 36a91eb..d97cbd3 100644 (file)
@@ -9,7 +9,7 @@
 #include <netinet/in.h>
 #include <sys/times.h>
 
-#include "backend.h"
+#include "upload_backend.h"
 
 enum tftp_opcode {
     TFTP_RRQ   = 1,
@@ -107,7 +107,7 @@ done:
     return err;
 }
 
-static int be_tftp_write(struct backend *be)
+static int upload_tftp_write(struct upload_backend *be)
 {
     static uint16_t local_port = 0x4000;
     struct tftp_state tftp;
@@ -170,9 +170,9 @@ static int be_tftp_write(struct backend *be)
     return 0;
 }
 
-struct backend be_tftp = {
+struct upload_backend upload_tftp = {
     .name       = "tftp",
     .helpmsg    = "filename [tftp_server]",
     .minargs    = 1,
-    .write      = be_tftp_write,
+    .write      = upload_tftp_write,
 };
similarity index 96%
rename from com32/sysdump/be_ymodem.c
rename to com32/libupload/upload_ymodem.c
index 316b3d4..c42327d 100644 (file)
@@ -5,7 +5,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <inttypes.h>
-#include "backend.h"
+#include "upload_backend.h"
 #include "serial.h"
 
 enum {
@@ -98,7 +98,7 @@ static void send_ack(struct ymodem_state *ym, const uint8_t *blk, size_t bytes)
     } while (ack_buf == NAK);
 }
 
-static int be_ymodem_write(struct backend *be)
+static int upload_ymodem_write(struct upload_backend *be)
 {
     static const uint8_t eot_buf = EOT;
     uint8_t ack_buf;
@@ -167,9 +167,9 @@ static int be_ymodem_write(struct backend *be)
     return 0;
 }
 
-struct backend be_ymodem = {
+struct upload_backend upload_ymodem = {
     .name       = "ymodem",
     .helpmsg    = "filename [port [speed]]",
     .minargs    = 1,
-    .write      = be_ymodem_write,
+    .write      = upload_ymodem_write,
 };
similarity index 87%
rename from com32/sysdump/zout.c
rename to com32/libupload/zout.c
index ece934c..b5575a9 100644 (file)
@@ -8,12 +8,12 @@
 #include <inttypes.h>
 #include <stdbool.h>
 #include <zlib.h>
-#include "backend.h"
+#include "upload_backend.h"
 #include "ctime.h"
 
 #define ALLOC_CHUNK    65536
 
-int init_data(struct backend *be, const char *argv[])
+int init_data(struct upload_backend *be, const char *argv[])
 {
     be->now = posix_time();
     be->argv = argv;
@@ -33,7 +33,7 @@ int init_data(struct backend *be, const char *argv[])
     return 0;
 }
 
-static int do_deflate(struct backend *be, int flush)
+static int do_deflate(struct upload_backend *be, int flush)
 {
     int rv;
     char *buf;
@@ -55,7 +55,7 @@ static int do_deflate(struct backend *be, int flush)
 }
 
 
-int write_data(struct backend *be, const void *buf, size_t len)
+int write_data(struct upload_backend *be, const void *buf, size_t len)
 {
     int rv = Z_OK;
 
@@ -75,7 +75,7 @@ int write_data(struct backend *be, const void *buf, size_t len)
 }
 
 /* Output the data and shut down the stream */
-int flush_data(struct backend *be)
+int flush_data(struct upload_backend *be)
 {
     int rv = Z_OK;
 
index bffee3a..b2093de 100644 (file)
@@ -19,7 +19,7 @@ topdir = ../..
 include ../MCONFIG
 -include $(topdir)/version.mk
 
-LIBS      = ../libutil/libutil_com.a ../lib/libcom32.a $(LIBGCC)
+LIBS      = ../libutil/libutil_com.a ../lib/libcom32.a ../libupload/libcom32upload.a $(LIBGCC)
 LNXLIBS           = ../libutil/libutil_lnx.a
 
 MODULES          = sysdump.c32
index 8671fc8..f2be4f4 100644 (file)
@@ -18,7 +18,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include "sysdump.h"
-#include "backend.h"
+#include "../libupload/upload_backend.h"
 #include "rbtree.h"
 
 struct acpi_rsdp {
@@ -151,7 +151,7 @@ static const struct acpi_rsdp *find_rsdp(void)
     return scan_for_rsdp(0xe0000, 0x100000);
 }
 
-static void dump_table(struct backend *be,
+static void dump_table(struct upload_backend *be,
                       const char name[], const void *ptr, uint32_t len)
 {
     char namebuf[64];
@@ -171,7 +171,7 @@ static void dump_table(struct backend *be,
     write_data(be, ptr, len);
 }
 
-static void dump_rsdt(struct backend *be, const struct acpi_rsdp *rsdp)
+static void dump_rsdt(struct upload_backend *be, const struct acpi_rsdp *rsdp)
 {
     const struct acpi_rsdt *rsdt;
     uint32_t i, n;
@@ -196,7 +196,7 @@ static void dump_rsdt(struct backend *be, const struct acpi_rsdp *rsdp)
     }
 }
 
-static void dump_xsdt(struct backend *be, const struct acpi_rsdp *rsdp)
+static void dump_xsdt(struct upload_backend *be, const struct acpi_rsdp *rsdp)
 {
     const struct acpi_xsdt *xsdt;
     uint32_t rsdp_len = rsdp->rev > 0 ? rsdp->len : 20;
@@ -231,7 +231,7 @@ static void dump_xsdt(struct backend *be, const struct acpi_rsdp *rsdp)
     }
 }
 
-void dump_acpi(struct backend *be)
+void dump_acpi(struct upload_backend *be)
 {
     const struct acpi_rsdp *rsdp;
     uint32_t rsdp_len;
diff --git a/com32/sysdump/backend.h b/com32/sysdump/backend.h
deleted file mode 100644 (file)
index f2b3bc2..0000000
+++ /dev/null
@@ -1,55 +0,0 @@
-#ifndef BACKEND_H
-#define BACKEND_H
-
-#include <stddef.h>
-#include <inttypes.h>
-#include <stdbool.h>
-#include <zlib.h>
-#include "serial.h"
-
-/* Backend flags */
-#define BE_NEEDLEN     0x01
-
-struct backend {
-    const char *name;
-    const char *helpmsg;
-    int minargs;
-
-    size_t dbytes;
-    size_t zbytes;
-    const char **argv;
-
-    uint32_t now;
-
-    int (*write)(struct backend *);
-
-    z_stream zstream;
-    char *outbuf;
-    size_t alloc;
-};
-
-/* zout.c */
-int init_data(struct backend *be, const char *argv[]);
-int write_data(struct backend *be, const void *buf, size_t len);
-int flush_data(struct backend *be);
-
-/* cpio.c */
-#define cpio_init init_data
-int cpio_hdr(struct backend *be, uint32_t mode, size_t datalen,
-            const char *filename);
-int cpio_mkdir(struct backend *be, const char *filename);
-int cpio_writefile(struct backend *be, const char *filename,
-                  const void *data, size_t len);
-int cpio_close(struct backend *be);
-#define MODE_FILE      0100644
-#define MODE_DIR       0040755
-
-/* backends.c */
-struct backend *get_backend(const char *name);
-
-/* backends */
-extern struct backend be_tftp;
-extern struct backend be_ymodem;
-extern struct backend be_srec;
-
-#endif /* BACKEND_H */
index 372a70d..53c54f2 100644 (file)
@@ -8,7 +8,7 @@
 #include <com32.h>
 #include <sys/cpu.h>
 #include "sysdump.h"
-#include "backend.h"
+#include "../libupload/upload_backend.h"
 
 struct cpuid_data {
     uint32_t eax, ebx, ecx, edx;
@@ -29,7 +29,7 @@ static void get_cpuid(uint32_t eax, uint32_t ecx, struct cpuid_data *data)
 
 #define CPUID_CHUNK 128
 
-void dump_cpuid(struct backend *be)
+void dump_cpuid(struct upload_backend *be)
 {
     struct cpuid_info *buf = NULL;
     int nentry, nalloc;
index be4cce4..64fcffb 100644 (file)
@@ -6,7 +6,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include "sysdump.h"
-#include "backend.h"
+#include "../libupload/upload_backend.h"
 
 struct dmi_header {
     char signature[5];
@@ -60,7 +60,7 @@ static bool is_smbios(size_t dptr)
        is_old_dmi(dptr+16);
 }
 
-static void dump_smbios(struct backend *be, size_t dptr)
+static void dump_smbios(struct upload_backend *be, size_t dptr)
 {
     const struct smbios_header *smb = (void *)dptr;
     struct smbios_header smx = *smb;
@@ -82,7 +82,7 @@ static void dump_smbios(struct backend *be, size_t dptr)
     write_data(be, (const void *)smb->dmi.tbladdr, smb->dmi.tbllen);
 }
 
-static void dump_old_dmi(struct backend *be, size_t dptr)
+static void dump_old_dmi(struct upload_backend *be, size_t dptr)
 {
     const struct dmi_header *dmi = (void *)dptr;
     struct fake {
@@ -108,7 +108,7 @@ static void dump_old_dmi(struct backend *be, size_t dptr)
     write_data(be, (const void *)dmi->tbladdr, dmi->tbllen);
 }
 
-void dump_dmi(struct backend *be)
+void dump_dmi(struct upload_backend *be)
 {
     size_t dptr;
 
index d0d40a7..4931024 100644 (file)
@@ -20,7 +20,7 @@
 #include <console.h>
 #include <sys/cpu.h>
 #include "../../version.h"
-#include "backend.h"
+#include "../libupload/upload_backend.h"
 #include "sysdump.h"
 
 const char program[] = "sysdump";
@@ -32,7 +32,7 @@ __noreturn die(const char *msg)
     exit(1);
 }
 
-static void dump_all(struct backend *be, const char *argv[])
+static void dump_all(struct upload_backend *be, const char *argv[])
 {
     cpio_init(be, argv);
 
@@ -50,20 +50,20 @@ static void dump_all(struct backend *be, const char *argv[])
     flush_data(be);
 }
 
-static struct backend *backends[] =
+static struct upload_backend *upload_backends[] =
 {
-    &be_tftp,
-    &be_ymodem,
-    &be_srec,
+    &upload_tftp,
+    &upload_ymodem,
+    &upload_srec,
     NULL
 };
 
 __noreturn usage(void)
 {
-    struct backend **bep, *be;
+    struct upload_backend **bep, *be;
 
     printf("Usage:\n");
-    for (bep = backends ; (be = *bep) ; bep++)
+    for (bep = upload_backends ; (be = *bep) ; bep++)
        printf("    %s %s %s\n", program, be->name, be->helpmsg);
 
     exit(1);
@@ -71,7 +71,7 @@ __noreturn usage(void)
 
 int main(int argc, char *argv[])
 {
-    struct backend **bep, *be;
+    struct upload_backend **bep, *be;
 
     openconsole(&dev_null_r, &dev_stdcon_w);
     fputs(version, stdout);
@@ -79,7 +79,7 @@ int main(int argc, char *argv[])
     if (argc < 2)
        usage();
 
-    for (bep = backends ; (be = *bep) ; bep++) {
+    for (bep = upload_backends ; (be = *bep) ; bep++) {
        if (!strcmp(be->name, argv[1]))
            break;
     }
index 251107d..7a21a31 100644 (file)
@@ -7,7 +7,7 @@
 #include <stdlib.h>
 #include <com32.h>
 #include "sysdump.h"
-#include "backend.h"
+#include "../libupload/upload_backend.h"
 
 #define E820_CHUNK 128
 struct e820_info {
@@ -16,7 +16,7 @@ struct e820_info {
     uint8_t  data[24];
 };
 
-static void dump_e820(struct backend *be)
+static void dump_e820(struct upload_backend *be)
 {
     com32sys_t ireg, oreg;
     struct e820_info *curr;
@@ -63,7 +63,7 @@ static void dump_e820(struct backend *be)
     lfree(curr);
 }
 
-void dump_memory_map(struct backend *be)
+void dump_memory_map(struct upload_backend *be)
 {
     com32sys_t ireg, oreg;
 
index 6552e7f..20b20c6 100644 (file)
@@ -7,7 +7,7 @@
 #include <stdlib.h>
 #include <sys/cpu.h>
 #include "sysdump.h"
-#include "backend.h"
+#include "../libupload/upload_backend.h"
 
 static char *lowmem;
 static size_t lowmem_len;
@@ -29,7 +29,7 @@ void snapshot_lowmem(void)
     }
 }
 
-static void dump_memory_range(struct backend *be, const void *where,
+static void dump_memory_range(struct upload_backend *be, const void *where,
                              const void *addr, size_t len)
 {
     char filename[32];
@@ -38,7 +38,7 @@ static void dump_memory_range(struct backend *be, const void *where,
     cpio_writefile(be, filename, where, len);
 }
 
-void dump_memory(struct backend *be)
+void dump_memory(struct upload_backend *be)
 {
     printf("Dumping memory... ");
 
index 1d68727..7646a75 100644 (file)
@@ -7,9 +7,9 @@
 #include <stdlib.h>
 #include <sys/pci.h>
 #include "sysdump.h"
-#include "backend.h"
+#include "../libupload/upload_backend.h"
 
-static void dump_pci_device(struct backend *be, pciaddr_t a, uint8_t hdrtype)
+static void dump_pci_device(struct upload_backend *be, pciaddr_t a, uint8_t hdrtype)
 {
     unsigned int bus  = pci_bus(a);
     unsigned int dev  = pci_dev(a);
@@ -31,7 +31,7 @@ static void dump_pci_device(struct backend *be, pciaddr_t a, uint8_t hdrtype)
     cpio_writefile(be, filename, data, sizeof data);
 }
 
-void dump_pci(struct backend *be)
+void dump_pci(struct upload_backend *be)
 {
     int cfgtype;
     unsigned int nbus, ndev, nfunc, maxfunc;
index a5b963f..15bb899 100644 (file)
@@ -1,15 +1,15 @@
 #ifndef SYSDUMP_H
 #define SYSDUMP_H
 
-struct backend;
+struct upload_backend;
 
-void dump_memory_map(struct backend *);
+void dump_memory_map(struct upload_backend *);
 void snapshot_lowmem(void);
-void dump_memory(struct backend *);
-void dump_dmi(struct backend *);
-void dump_acpi(struct backend *);
-void dump_cpuid(struct backend *);
-void dump_pci(struct backend *);
-void dump_vesa_tables(struct backend *);
+void dump_memory(struct upload_backend *);
+void dump_dmi(struct upload_backend *);
+void dump_acpi(struct upload_backend *);
+void dump_cpuid(struct upload_backend *);
+void dump_pci(struct upload_backend *);
+void dump_vesa_tables(struct upload_backend *);
 
 #endif /* SYSDUMP_H */
index 017f9e4..aebed18 100644 (file)
@@ -1,10 +1,10 @@
 #include <string.h>
 #include <stdio.h>
 #include "../lib/sys/vesa/vesa.h"
-#include "backend.h"
+#include "../libupload/upload_backend.h"
 #include "sysdump.h"
 
-void dump_vesa_tables(struct backend *be)
+void dump_vesa_tables(struct upload_backend *be)
 {
     com32sys_t rm;
     struct vesa_info *vip;