]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/redboot/v2_0/src/gunzip.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / redboot / v2_0 / src / gunzip.c
1 //==========================================================================
2 //
3 //      gunzip.c
4 //
5 //      RedBoot GZIP uncompress command
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 2005 eCosCentric Ltd
12 //
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
16 //
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 //
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
32 //
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 // -------------------------------------------
36 //####ECOSGPLCOPYRIGHTEND####
37 //==========================================================================
38 //#####DESCRIPTIONBEGIN####
39 //
40 // Author(s):    Peter Korsgaard
41 // Contributors: Peter Korsgaard
42 // Date:         2005-04-04
43 // Purpose:      
44 // Description:  
45 //              
46 // This code is part of RedBoot (tm).
47 //
48 //####DESCRIPTIONEND####
49 //
50 //==========================================================================
51
52 #include <redboot.h>
53
54 RedBoot_cmd("gunzip",
55             "Uncompress GZIP compressed data",
56             "-s <location> -d <location>",
57             do_gunzip
58     );
59
60 void
61 do_gunzip(int argc, char *argv[])
62 {
63     struct option_info opts[2];
64     unsigned long src, dst;
65     bool src_set, dst_set;
66     _pipe_t pipe;
67     _pipe_t* p = &pipe;
68     int err;
69
70     init_opts(&opts[0], 's', true, OPTION_ARG_TYPE_NUM, 
71               (void *)&src, (bool *)&src_set, "source address");
72     init_opts(&opts[1], 'd', true, OPTION_ARG_TYPE_NUM, 
73               (void *)&dst, (bool *)&dst_set, "destination address");
74     if (!scan_opts(argc, argv, 1, opts, 2, 0, 0, "")) {
75         return;
76     }
77
78     // Must have src and dst
79     if (!src_set || !dst_set) {
80         // try to use load_address for src 
81         if (dst_set 
82             && load_address >= (CYG_ADDRESS)ram_start 
83             && load_address < load_address_end) {
84             src = load_address;
85             diag_printf("Decompressing from %p to %p\n",
86                         (void*)src, (void*)dst);
87         }
88         else
89         {
90             diag_printf("usage: gunzip -s <addr> -d <addr>\n");
91             return;
92         }
93     }
94
95     p->out_buf = (unsigned char*)dst;
96     p->out_max = p->out_size = -1;
97     p->in_buf = (unsigned char*)src;
98     p->in_avail = -1;
99
100     err = (*_dc_init)(p);
101
102     if (0 == err)
103         err = (*_dc_inflate)(p);
104
105     // Free used resources, do final translation of error value.
106     err = (*_dc_close)(p, err);
107
108     if (0 != err && p->msg) {
109        entry_address = (CYG_ADDRESS)NO_MEMORY;
110        diag_printf("Decompression error: %s\n", p->msg);
111     } else {
112         load_address     = entry_address = (CYG_ADDRESS)dst;
113         load_address_end = (CYG_ADDRESS)p->out_buf;
114         diag_printf("Decompressed %lu bytes\n", 
115                     load_address_end - load_address);
116     }
117 }