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 /**
38  * Set field within a CPIO header
39  *
40  * @v field Field within CPIO header
41  * @v value Value to set
42  */
43 void cpio_set_field ( char *field, unsigned long value ) {
44  char buf[9];
45 
46  snprintf ( buf, sizeof ( buf ), "%08lx", value );
47  memcpy ( field, buf, 8 );
48 }
49 
50 /**
51  * Get CPIO image filename
52  *
53  * @v image Image
54  * @ret len CPIO filename length (0 for no filename)
55  */
56 size_t cpio_name_len ( struct image *image ) {
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 }
71 
72 /**
73  * Parse CPIO image parameters
74  *
75  * @v image Image
76  * @v cpio CPIO header to fill in
77  */
78 static void cpio_parse_cmdline ( struct image *image,
79  struct cpio_header *cpio ) {
80  const char *cmdline;
81  char *arg;
82  char *end;
83  unsigned int mode;
84 
85  /* Skip image filename */
86  cmdline = ( cpio_name ( image ) + cpio_name_len ( image ) );
87 
88  /* Look for "mode=" */
89  if ( ( arg = strstr ( cmdline, "mode=" ) ) ) {
90  arg += 5;
91  mode = strtoul ( arg, &end, 8 /* Octal for file mode */ );
92  if ( *end && ( *end != ' ' ) ) {
93  DBGC ( image, "CPIO %p strange \"mode=\" "
94  "terminator '%c'\n", image, *end );
95  }
96  cpio_set_field ( cpio->c_mode, ( 0100000 | mode ) );
97  }
98 }
99 
100 /**
101  * Construct CPIO header for image, if applicable
102  *
103  * @v image Image
104  * @v cpio CPIO header to fill in
105  * @ret len Length of magic CPIO header (including filename)
106  */
107 size_t cpio_header ( struct image *image, struct cpio_header *cpio ) {
108  size_t name_len;
109  size_t len;
110 
111  /* Get filename length */
112  name_len = cpio_name_len ( image );
113 
114  /* Images with no filename are assumed to already be CPIO archives */
115  if ( ! name_len )
116  return 0;
117 
118  /* Construct CPIO header */
119  memset ( cpio, '0', sizeof ( *cpio ) );
120  memcpy ( cpio->c_magic, CPIO_MAGIC, sizeof ( cpio->c_magic ) );
121  cpio_set_field ( cpio->c_mode, 0100644 );
122  cpio_set_field ( cpio->c_nlink, 1 );
123  cpio_set_field ( cpio->c_filesize, image->len );
124  cpio_set_field ( cpio->c_namesize, ( name_len + 1 /* NUL */ ) );
125  cpio_parse_cmdline ( image, cpio );
126 
127  /* Calculate total length */
128  len = ( ( sizeof ( *cpio ) + name_len + 1 /* NUL */ + CPIO_ALIGN - 1 )
129  & ~( CPIO_ALIGN - 1 ) );
130 
131  return len;
132 }
size_t cpio_name_len(struct image *image)
Get CPIO image filename.
Definition: cpio.c:56
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
const char * name
Definition: ath9k_hw.c:1984
unsigned long strtoul(const char *string, char **endp, int base)
Convert string to numeric value.
Definition: string.c:471
char c_filesize[8]
Size of data field.
Definition: cpio.h:35
A CPIO archive header.
Definition: cpio.h:19
__SIZE_TYPE__ size_t
Definition: stdint.h:6
#define DBGC(...)
Definition: compiler.h:505
#define CPIO_ALIGN
CPIO header length alignment.
Definition: cpio.h:54
An executable image.
Definition: image.h:24
CPIO archives.
void cpio_set_field(char *field, unsigned long value)
Set field within a CPIO header.
Definition: cpio.c:43
char * strstr(const char *haystack, const char *needle)
Find substring.
Definition: string.c:309
void * memcpy(void *dest, const void *src, size_t len) __nonnull
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
char c_mode[8]
File mode and permissions.
Definition: cpio.h:25
size_t len
Length of raw file image.
Definition: image.h:43
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
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
uint32_t end
Ending offset.
Definition: netvsc.h:18
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
uint32_t cmdline
Definition: multiboot.h:16
String functions.
void * memset(void *dest, int character, size_t len) __nonnull
size_t cpio_header(struct image *image, struct cpio_header *cpio)
Construct CPIO header for image, if applicable.
Definition: cpio.c:107
static const char * cpio_name(struct image *image)
Get CPIO image name.
Definition: cpio.h:66