iPXE
Data Structures | Macros | Functions
cpio.h File Reference

CPIO archives. More...

#include <ipxe/image.h>

Go to the source code of this file.

Data Structures

struct  cpio_header
 A CPIO archive header. More...
 

Macros

#define CPIO_MAGIC   "070701"
 CPIO magic. More...
 
#define CPIO_ALIGN   4
 CPIO header length alignment. More...
 
#define INITRD_ALIGN   4096
 Alignment for CPIO archives within an initrd. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static const char * cpio_name (struct image *image)
 Get CPIO image name. More...
 
void cpio_set_field (char *field, unsigned long value)
 Set field within a CPIO header. More...
 
size_t cpio_name_len (struct image *image)
 Get CPIO image filename. More...
 
size_t cpio_header (struct image *image, struct cpio_header *cpio)
 Construct CPIO header for image, if applicable. More...
 

Detailed Description

CPIO archives.

Definition in file cpio.h.

Macro Definition Documentation

◆ CPIO_MAGIC

#define CPIO_MAGIC   "070701"

CPIO magic.

Definition at line 51 of file cpio.h.

◆ CPIO_ALIGN

#define CPIO_ALIGN   4

CPIO header length alignment.

Definition at line 54 of file cpio.h.

◆ INITRD_ALIGN

#define INITRD_ALIGN   4096

Alignment for CPIO archives within an initrd.

Definition at line 57 of file cpio.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ cpio_name()

static const char* cpio_name ( struct image image)
inlinestatic

Get CPIO image name.

Parameters
imageImage
Return values
nameImage name (not NUL terminated)

Definition at line 66 of file cpio.h.

66  {
67  return image->cmdline;
68 }
An executable image.
Definition: image.h:24
char * cmdline
Command line to pass to image.
Definition: image.h:39

References image::cmdline.

Referenced by bzimage_load_initrd(), cpio_name_len(), and efi_file_read_initrd().

◆ cpio_set_field()

void cpio_set_field ( char *  field,
unsigned long  value 
)

Set field within a CPIO header.

Parameters
fieldField within CPIO header
valueValue to set

Definition at line 43 of file cpio.c.

43  {
44  char buf[9];
45 
46  snprintf ( buf, sizeof ( buf ), "%08lx", value );
47  memcpy ( field, buf, 8 );
48 }
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(), and cpio_parse_cmdline().

◆ cpio_name_len()

size_t cpio_name_len ( struct image image)

Get CPIO image filename.

Parameters
imageImage
Return values
lenCPIO filename length (0 for no filename)

Definition at line 56 of file cpio.c.

56  {
57  const char *name = cpio_name ( image );
58  char *sep;
59  size_t len;
60 
61  /* Check for existence of CPIO filename */
62  if ( ! name )
63  return 0;
64 
65  /* Locate separator (if any) */
66  sep = strchr ( name, ' ' );
67  len = ( sep ? ( ( size_t ) ( sep - name ) ) : strlen ( name ) );
68 
69  return len;
70 }
const char * name
Definition: ath9k_hw.c:1984
__SIZE_TYPE__ size_t
Definition: stdint.h:6
An executable image.
Definition: image.h:24
char * strchr(const char *src, int character)
Find character within a string.
Definition: string.c:271
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
uint32_t len
Length.
Definition: ena.h:14
static const char * cpio_name(struct image *image)
Get CPIO image name.
Definition: cpio.h:66

References cpio_name(), len, name, strchr(), and strlen().

Referenced by bzimage_load_initrd(), cpio_header(), and efi_file_read_initrd().

◆ cpio_header()

size_t cpio_header ( struct image image,
struct cpio_header cpio 
)

Construct CPIO header for image, if applicable.

Parameters
imageImage
cpioCPIO header to fill in
Return values
lenLength of magic CPIO header (including filename)

Definition at line 102 of file cpio.c.

102  {
103  size_t name_len;
104  size_t len;
105 
106  /* Get filename length */
107  name_len = cpio_name_len ( image );
108 
109  /* Images with no filename are assumed to already be CPIO archives */
110  if ( ! name_len )
111  return 0;
112 
113  /* Construct CPIO header */
114  memset ( cpio, '0', sizeof ( *cpio ) );
115  memcpy ( cpio->c_magic, CPIO_MAGIC, sizeof ( cpio->c_magic ) );
116  cpio_set_field ( cpio->c_mode, 0100644 );
117  cpio_set_field ( cpio->c_nlink, 1 );
118  cpio_set_field ( cpio->c_filesize, image->len );
119  cpio_set_field ( cpio->c_namesize, ( name_len + 1 /* NUL */ ) );
120  cpio_parse_cmdline ( image, cpio );
121 
122  /* Calculate total length */
123  len = ( ( sizeof ( *cpio ) + name_len + 1 /* NUL */ + CPIO_ALIGN - 1 )
124  & ~( CPIO_ALIGN - 1 ) );
125 
126  return len;
127 }
size_t cpio_name_len(struct image *image)
Get CPIO image filename.
Definition: cpio.c:56
char c_filesize[8]
Size of data field.
Definition: cpio.h:35
#define CPIO_ALIGN
CPIO header length alignment.
Definition: cpio.h:54
An executable image.
Definition: image.h:24
void cpio_set_field(char *field, unsigned long value)
Set field within a CPIO header.
Definition: cpio.c:43
void * memcpy(void *dest, const void *src, size_t len) __nonnull
char c_mode[8]
File mode and permissions.
Definition: cpio.h:25
size_t len
Length of raw file image.
Definition: image.h:43
static void cpio_parse_cmdline(struct image *image, struct cpio_header *cpio)
Parse CPIO image parameters.
Definition: cpio.c:78
char c_namesize[8]
Length of filename, including final NUL.
Definition: cpio.h:45
char c_nlink[8]
Number of links.
Definition: cpio.h:31
char c_magic[6]
The string "070701" or "070702".
Definition: cpio.h:21
uint32_t len
Length.
Definition: ena.h:14
#define CPIO_MAGIC
CPIO magic.
Definition: cpio.h:51
void * memset(void *dest, int character, size_t len) __nonnull

References cpio_header::c_filesize, cpio_header::c_magic, cpio_header::c_mode, cpio_header::c_namesize, cpio_header::c_nlink, CPIO_ALIGN, CPIO_MAGIC, cpio_name_len(), cpio_parse_cmdline(), cpio_set_field(), len, image::len, memcpy(), and memset().

Referenced by bzimage_load_initrd(), and efi_file_read_initrd().