cpio.c 3.09 KB
Newer Older
longpanda's avatar
longpanda committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

/**
 * @file
 *
 * CPIO archives
 *
 */

#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wimboot.h"
#include "cpio.h"

/**
 * Align CPIO length to nearest dword
 *
 * @v len		Length
 * @ret len		Aligned length
 */
static size_t cpio_align ( size_t len ) {
	return ( ( len + 0x03 ) & ~0x03 );
}

/**
 * Parse CPIO field value
 *
 * @v field		ASCII field
 * @ret value		Field value
 */
static unsigned long cpio_value ( const char *field ) {
	char buf[9];

	memcpy ( buf, field, ( sizeof ( buf ) - 1 ) );
	buf[ sizeof ( buf ) - 1 ] = '\0';
	return strtoul ( buf, NULL, 16 );
}

/**
 * Extract files from CPIO archive
 *
 * @v data		CPIO archive
 * @v len		Maximum length of CPIO archive
 * @v file		File handler
 * @ret rc		Return status code
 */
int cpio_extract ( void *data, size_t len,
		   int ( * file ) ( const char *name, void *data,
				    size_t len ) ) {
	const struct cpio_header *cpio;
	const uint32_t *pad;
	const char *file_name;
	void *file_data;
	size_t file_name_len;
	size_t file_len;
	size_t cpio_len;
	int rc;

	while ( 1 ) {

		/* Skip over any padding */
		while ( len >= sizeof ( *pad ) ) {
			pad = data;
			if ( *pad )
				break;
			data += sizeof ( *pad );
			len -= sizeof ( *pad );
		}

		/* Stop if we have reached the end of the archive */
		if ( ! len )
			return 0;

		/* Sanity check */
		if ( len < sizeof ( *cpio ) ) {
			DBG ( "Truncated CPIO header\n" );
			return -1;
		}
		cpio = data;

		/* Check magic */
		if ( memcmp ( cpio->c_magic, CPIO_MAGIC,
			      sizeof ( cpio->c_magic ) ) != 0 ) {
			DBG ( "Bad CPIO magic\n" );
			return -1;
		}

		/* Extract file parameters */
		file_name = ( ( void * ) ( cpio + 1 ) );
		file_name_len = cpio_value ( cpio->c_namesize );
		file_data = ( data + cpio_align ( sizeof ( *cpio ) +
						  file_name_len ) );
		file_len = cpio_value ( cpio->c_filesize );
		cpio_len = ( file_data + file_len - data );
		if ( cpio_len < len )
			cpio_len = cpio_align ( cpio_len );
		if ( cpio_len > len ) {
			DBG ( "Truncated CPIO file\n" );
			return -1;
		}

		/* If we reach the trailer, we're done */
		if ( strcmp ( file_name, CPIO_TRAILER ) == 0 )
			return 0;

		/* Process file */
		if ( ( rc = file ( file_name, file_data, file_len ) ) != 0 )
			return rc;

		/* Move to next file */
		data += cpio_len;
		len -= cpio_len;
	}
}