iPXE
cpio.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /** @file
27  *
28  * CPIO archives
29  *
30  */
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <ipxe/cpio.h>
36 
37 /** CPIO default file mode */
38 #define CPIO_DEFAULT_MODE 0644
39 
40 /** CPIO directory mode */
41 #define CPIO_DEFAULT_DIR_MODE 0755
42 
43 /**
44  * Set field within a CPIO header
45  *
46  * @v field Field within CPIO header
47  * @v value Value to set
48  */
49 static void cpio_set_field ( char *field, unsigned long value ) {
50  char buf[9];
51 
52  snprintf ( buf, sizeof ( buf ), "%08lx", value );
53  memcpy ( field, buf, 8 );
54 }
55 
56 /**
57  * Get maximum number of CPIO headers (i.e. number of path components)
58  *
59  * @v image Image
60  * @ret max Maximum number of CPIO headers
61  */
62 static unsigned int cpio_max ( struct image *image ) {
63  const char *name = cpio_name ( image );
64  unsigned int max = 0;
65  char c;
66  char p;
67 
68  /* Check for existence of CPIO filename */
69  if ( ! name )
70  return 0;
71 
72  /* Count number of path components */
73  for ( p = '/' ; ( ( ( c = *(name++) ) ) && ( c != ' ' ) ) ; p = c ) {
74  if ( ( p == '/' ) && ( c != '/' ) )
75  max++;
76  }
77 
78  return max;
79 }
80 
81 /**
82  * Get CPIO image filename
83  *
84  * @v image Image
85  * @v depth Path depth
86  * @ret len Filename length
87  */
88 static size_t cpio_name_len ( struct image *image, unsigned int depth ) {
89  const char *name = cpio_name ( image );
90  size_t len;
91  char c;
92  char p;
93 
94  /* Sanity checks */
95  assert ( name != NULL );
96  assert ( depth > 0 );
97 
98  /* Calculate length up to specified path depth */
99  for ( len = 0, p = '/' ; ( ( ( c = name[len] ) ) && ( c != ' ' ) ) ;
100  len++, p = c ) {
101  if ( ( c == '/' ) && ( p != '/' ) && ( --depth == 0 ) )
102  break;
103  }
104 
105  return len;
106 }
107 
108 /**
109  * Parse CPIO image parameters
110  *
111  * @v image Image
112  * @v mode Mode to fill in
113  * @v count Number of CPIO headers to fill in
114  */
115 static void cpio_parse_cmdline ( struct image *image, unsigned int *mode,
116  unsigned int *count ) {
117  const char *arg;
118  char *end;
119 
120  /* Set default values */
122  *count = 1;
123 
124  /* Parse "mode=...", if present */
125  if ( ( arg = image_argument ( image, "mode=" ) ) ) {
126  *mode = strtoul ( arg, &end, 8 /* Octal for file mode */ );
127  if ( *end && ( *end != ' ' ) ) {
128  DBGC ( image, "CPIO %s strange \"mode=\" "
129  "terminator '%c'\n", image->name, *end );
130  }
131  }
132 
133  /* Parse "mkdir=...", if present */
134  if ( ( arg = image_argument ( image, "mkdir=" ) ) ) {
135  *count += strtoul ( arg, &end, 10 );
136  if ( *end && ( *end != ' ' ) ) {
137  DBGC ( image, "CPIO %s strange \"mkdir=\" "
138  "terminator '%c'\n", image->name, *end );
139  }
140  }
141 
142  /* Allow "mkdir=-1" to request creation of full directory tree */
143  if ( ! *count )
144  *count = -1U;
145 }
146 
147 /**
148  * Construct CPIO header for image, if applicable
149  *
150  * @v image Image
151  * @v index CPIO header index
152  * @v cpio CPIO header to fill in
153  * @ret len Length of CPIO header (including name, excluding NUL)
154  */
155 size_t cpio_header ( struct image *image, unsigned int index,
156  struct cpio_header *cpio ) {
157  const char *name = cpio_name ( image );
158  unsigned int mode;
159  unsigned int count;
160  unsigned int max;
161  unsigned int depth;
162  unsigned int i;
163  size_t name_len;
164  size_t len;
165 
166  /* Parse command line arguments */
168 
169  /* Determine number of CPIO headers to be constructed */
170  max = cpio_max ( image );
171  if ( count > max )
172  count = max;
173 
174  /* Determine path depth of this CPIO header */
175  if ( index >= count )
176  return 0;
177  depth = ( max - count + index + 1 );
178 
179  /* Get filename length */
180  name_len = cpio_name_len ( image, depth );
181 
182  /* Set directory mode or file mode as appropriate */
183  if ( name[name_len] == '/' ) {
185  } else {
186  mode |= CPIO_MODE_FILE;
187  }
188 
189  /* Set length on final header */
190  len = ( ( depth < max ) ? 0 : image->len );
191 
192  /* Construct CPIO header */
193  memset ( cpio, '0', sizeof ( *cpio ) );
194  memcpy ( cpio->c_magic, CPIO_MAGIC, sizeof ( cpio->c_magic ) );
195  cpio_set_field ( cpio->c_mode, mode );
196  cpio_set_field ( cpio->c_nlink, 1 );
197  cpio_set_field ( cpio->c_filesize, len );
198  cpio_set_field ( cpio->c_namesize, ( name_len + 1 /* NUL */ ) );
199  DBGC ( image, "CPIO %s %d/%d \"", image->name, depth, max );
200  for ( i = 0 ; i < name_len ; i++ )
201  DBGC ( image, "%c", name[i] );
202  DBGC ( image, "\"\n" );
203  DBGC2_HDA ( image, 0, cpio, sizeof ( *cpio ) );
204 
205  /* Calculate total length */
206  return ( sizeof ( *cpio ) + name_len );
207 }
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
const char * name
Definition: ath9k_hw.c:1984
#define max(x, y)
Definition: ath.h:40
unsigned long strtoul(const char *string, char **endp, int base)
Convert string to numeric value.
Definition: string.c:484
char c_filesize[8]
Size of data field.
Definition: cpio.h:36
A CPIO archive header.
Definition: cpio.h:20
#define CPIO_MODE_FILE
CPIO type for regular files.
Definition: cpio.h:55
uint16_t mode
Acceleration mode.
Definition: ena.h:26
#define DBGC(...)
Definition: compiler.h:505
long index
Definition: bigint.h:62
An executable image.
Definition: image.h:23
#define CPIO_DEFAULT_DIR_MODE
CPIO directory mode.
Definition: cpio.c:41
#define CPIO_DEFAULT_MODE
CPIO default file mode.
Definition: cpio.c:38
CPIO archives.
static size_t cpio_name_len(struct image *image, unsigned int depth)
Get CPIO image filename.
Definition: cpio.c:88
void * memcpy(void *dest, const void *src, size_t len) __nonnull
size_t cpio_header(struct image *image, unsigned int index, struct cpio_header *cpio)
Construct CPIO header for image, if applicable.
Definition: cpio.c:155
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
ring len
Length.
Definition: dwmac.h:231
static unsigned int cpio_max(struct image *image)
Get maximum number of CPIO headers (i.e.
Definition: cpio.c:62
static unsigned int count
Number of entries.
Definition: dwmac.h:225
#define DBGC2_HDA(...)
Definition: compiler.h:523
static void cpio_set_field(char *field, unsigned long value)
Set field within a CPIO header.
Definition: cpio.c:49
char c_mode[8]
File mode and permissions.
Definition: cpio.h:26
size_t len
Length of raw file image.
Definition: image.h:55
char c_namesize[8]
Length of filename, including final NUL.
Definition: cpio.h:46
char c_nlink[8]
Number of links.
Definition: cpio.h:32
char c_magic[6]
The string "070701" or "070702".
Definition: cpio.h:22
#define CPIO_MAGIC
CPIO magic.
Definition: cpio.h:52
const char * image_argument(struct image *image, const char *key)
Find argument within image command line.
Definition: image.c:652
uint32_t end
Ending offset.
Definition: netvsc.h:18
static void cpio_parse_cmdline(struct image *image, unsigned int *mode, unsigned int *count)
Parse CPIO image parameters.
Definition: cpio.c:115
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
#define CPIO_MODE_DIR
CPIO type for directories.
Definition: cpio.h:58
char * name
Name.
Definition: image.h:37
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
void * memset(void *dest, int character, size_t len) __nonnull
static const char * cpio_name(struct image *image)
Get CPIO image name.
Definition: cpio.h:70