iPXE
Macros | Functions
cpio.c File Reference

CPIO archives. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ipxe/cpio.h>

Go to the source code of this file.

Macros

#define CPIO_DEFAULT_MODE   0644
 CPIO default file mode. More...
 
#define CPIO_DEFAULT_DIR_MODE   0755
 CPIO directory mode. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static void cpio_set_field (char *field, unsigned long value)
 Set field within a CPIO header. More...
 
static unsigned int cpio_max (struct image *image)
 Get maximum number of CPIO headers (i.e. More...
 
static size_t cpio_name_len (struct image *image, unsigned int depth)
 Get CPIO image filename. More...
 
static void cpio_parse_cmdline (struct image *image, unsigned int *mode, unsigned int *count)
 Parse CPIO image parameters. More...
 
size_t cpio_header (struct image *image, unsigned int index, struct cpio_header *cpio)
 Construct CPIO header for image, if applicable. More...
 

Detailed Description

CPIO archives.

Definition in file cpio.c.

Macro Definition Documentation

◆ CPIO_DEFAULT_MODE

#define CPIO_DEFAULT_MODE   0644

CPIO default file mode.

Definition at line 38 of file cpio.c.

◆ CPIO_DEFAULT_DIR_MODE

#define CPIO_DEFAULT_DIR_MODE   0755

CPIO directory mode.

Definition at line 41 of file cpio.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ cpio_set_field()

static void cpio_set_field ( char *  field,
unsigned long  value 
)
static

Set field within a CPIO header.

Parameters
fieldField within CPIO header
valueValue to set

Definition at line 49 of file cpio.c.

49  {
50  char buf[9];
51 
52  snprintf ( buf, sizeof ( buf ), "%08lx", value );
53  memcpy ( field, buf, 8 );
54 }
void * memcpy(void *dest, const void *src, size_t len) __nonnull
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382

References memcpy(), snprintf(), and value.

Referenced by cpio_header().

◆ cpio_max()

static unsigned int cpio_max ( struct image image)
static

Get maximum number of CPIO headers (i.e.

number of path components)

Parameters
imageImage
Return values
maxMaximum number of CPIO headers

Definition at line 62 of file cpio.c.

62  {
63  const char *name = cpio_name ( image );
64  unsigned int max = 0;
65  char c;
66 
67  /* Check for existence of CPIO filename */
68  if ( ! name )
69  return 0;
70 
71  /* Count number of path separators */
72  while ( ( ( c = *(name++) ) ) && ( c != ' ' ) ) {
73  if ( c == '/' )
74  max++;
75  }
76 
77  return max;
78 }
const char * name
Definition: ath9k_hw.c:1984
static __always_inline void off_t int c
Definition: librm.h:173
#define max(x, y)
Definition: ath.h:39
An executable image.
Definition: image.h:24
static const char * cpio_name(struct image *image)
Get CPIO image name.
Definition: cpio.h:73

References c, cpio_name(), max, and name.

Referenced by cpio_header().

◆ cpio_name_len()

static size_t cpio_name_len ( struct image image,
unsigned int  depth 
)
static

Get CPIO image filename.

Parameters
imageImage
depthPath depth
Return values
lenFilename length

Definition at line 87 of file cpio.c.

87  {
88  const char *name = cpio_name ( image );
89  size_t len;
90  char c;
91 
92  /* Sanity check */
93  assert ( name != NULL );
94 
95  /* Calculate length up to specified path depth */
96  for ( len = 0 ; ( ( ( c = name[len] ) ) && ( c != ' ' ) ) ; len++ ) {
97  if ( ( c == '/' ) && ( depth-- == 0 ) )
98  break;
99  }
100 
101  return len;
102 }
const char * name
Definition: ath9k_hw.c:1984
static __always_inline void off_t int c
Definition: librm.h:173
An executable image.
Definition: image.h:24
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
uint32_t len
Length.
Definition: ena.h:14
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
static const char * cpio_name(struct image *image)
Get CPIO image name.
Definition: cpio.h:73

References assert(), c, cpio_name(), len, name, and NULL.

Referenced by cpio_header().

◆ cpio_parse_cmdline()

static void cpio_parse_cmdline ( struct image image,
unsigned int *  mode,
unsigned int *  count 
)
static

Parse CPIO image parameters.

Parameters
imageImage
modeMode to fill in
countNumber of CPIO headers to fill in

Definition at line 111 of file cpio.c.

112  {
113  const char *arg;
114  char *end;
115 
116  /* Set default values */
117  *mode = CPIO_DEFAULT_MODE;
118  *count = 1;
119 
120  /* Parse "mode=...", if present */
121  if ( ( arg = image_argument ( image, "mode=" ) ) ) {
122  *mode = strtoul ( arg, &end, 8 /* Octal for file mode */ );
123  if ( *end && ( *end != ' ' ) ) {
124  DBGC ( image, "CPIO %p strange \"mode=\" "
125  "terminator '%c'\n", image, *end );
126  }
127  }
128 
129  /* Parse "mkdir=...", if present */
130  if ( ( arg = image_argument ( image, "mkdir=" ) ) ) {
131  *count += strtoul ( arg, &end, 10 );
132  if ( *end && ( *end != ' ' ) ) {
133  DBGC ( image, "CPIO %p strange \"mkdir=\" "
134  "terminator '%c'\n", image, *end );
135  }
136  }
137 
138  /* Allow "mkdir=-1" to request creation of full directory tree */
139  if ( ! *count )
140  *count = -1U;
141 }
unsigned long strtoul(const char *string, char **endp, int base)
Convert string to numeric value.
Definition: string.c:484
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
#define CPIO_DEFAULT_MODE
CPIO default file mode.
Definition: cpio.c:38
uint16_t count
Number of entries.
Definition: ena.h:22
const char * image_argument(struct image *image, const char *key)
Find argument within image command line.
Definition: image.c:612
uint32_t end
Ending offset.
Definition: netvsc.h:18

References count, CPIO_DEFAULT_MODE, DBGC, end, image_argument(), and strtoul().

Referenced by cpio_header().

◆ cpio_header()

size_t cpio_header ( struct image image,
unsigned int  index,
struct cpio_header cpio 
)

Construct CPIO header for image, if applicable.

Parameters
imageImage
indexCPIO header index
cpioCPIO header to fill in
Return values
lenLength of CPIO header (including name, excluding NUL)

Definition at line 151 of file cpio.c.

152  {
153  const char *name = cpio_name ( image );
154  unsigned int mode;
155  unsigned int count;
156  unsigned int max;
157  unsigned int depth;
158  unsigned int i;
159  size_t name_len;
160  size_t len;
161 
162  /* Parse command line arguments */
163  cpio_parse_cmdline ( image, &mode, &count );
164 
165  /* Determine number of CPIO headers to be constructed */
166  max = cpio_max ( image );
167  if ( count > max )
168  count = max;
169 
170  /* Determine path depth of this CPIO header */
171  if ( index >= count )
172  return 0;
173  depth = ( max - count + index + 1 );
174 
175  /* Get filename length */
176  name_len = cpio_name_len ( image, depth );
177 
178  /* Calculate mode and length */
179  if ( depth < max ) {
180  /* Directory */
182  len = 0;
183  } else {
184  /* File */
185  mode |= CPIO_MODE_FILE;
186  len = image->len;
187  }
188 
189  /* Construct CPIO header */
190  memset ( cpio, '0', sizeof ( *cpio ) );
191  memcpy ( cpio->c_magic, CPIO_MAGIC, sizeof ( cpio->c_magic ) );
192  cpio_set_field ( cpio->c_mode, mode );
193  cpio_set_field ( cpio->c_nlink, 1 );
194  cpio_set_field ( cpio->c_filesize, len );
195  cpio_set_field ( cpio->c_namesize, ( name_len + 1 /* NUL */ ) );
196  DBGC ( image, "CPIO %s %d/%d \"", image->name, depth, max );
197  for ( i = 0 ; i < name_len ; i++ )
198  DBGC ( image, "%c", name[i] );
199  DBGC ( image, "\"\n" );
200  DBGC2_HDA ( image, 0, cpio, sizeof ( *cpio ) );
201 
202  /* Calculate total length */
203  return ( sizeof ( *cpio ) + name_len );
204 }
const char * name
Definition: ath9k_hw.c:1984
#define max(x, y)
Definition: ath.h:39
char c_filesize[8]
Size of data field.
Definition: cpio.h:36
#define CPIO_MODE_FILE
CPIO type for regular files.
Definition: cpio.h:55
#define DBGC(...)
Definition: compiler.h:505
long index
Definition: bigint.h:62
An executable image.
Definition: image.h:24
#define CPIO_DEFAULT_DIR_MODE
CPIO directory mode.
Definition: cpio.c:41
static size_t cpio_name_len(struct image *image, unsigned int depth)
Get CPIO image filename.
Definition: cpio.c:87
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static unsigned int cpio_max(struct image *image)
Get maximum number of CPIO headers (i.e.
Definition: cpio.c:62
uint16_t count
Number of entries.
Definition: ena.h:22
#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:43
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
static void cpio_parse_cmdline(struct image *image, unsigned int *mode, unsigned int *count)
Parse CPIO image parameters.
Definition: cpio.c:111
#define CPIO_MODE_DIR
CPIO type for directories.
Definition: cpio.h:58
char * name
Name.
Definition: image.h:34
uint32_t len
Length.
Definition: ena.h:14
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:73

References cpio_header::c_filesize, cpio_header::c_magic, cpio_header::c_mode, cpio_header::c_namesize, cpio_header::c_nlink, count, CPIO_DEFAULT_DIR_MODE, CPIO_MAGIC, cpio_max(), CPIO_MODE_DIR, CPIO_MODE_FILE, cpio_name(), cpio_name_len(), cpio_parse_cmdline(), cpio_set_field(), DBGC, DBGC2_HDA, index, len, image::len, max, memcpy(), memset(), image::name, and name.

Referenced by bzimage_load_initrd(), and efi_file_read_initrd().