/* * add_header.c - partially based on OpenWrt's motorola-bin.c * * Copyright (C) 2008 Imre Kaloz * Gabor Juhos * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License, * version 2 as published by the Free Software Foundation. * * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* * The add_header utility used by various vendors preprends the buf * image with a header containing a CRC32 value which is generated for the * model id + reserved space for CRC32 + buf, then replaces the reserved * area with the actual CRC32. This replacement tool mimics this behavior. */ #include #include #include #include #include #include #include #include #include #include #define BPB 8 /* bits/byte */ static uint32_t crc32[1<> 1)) : (crc >> 1); crc32[n] = crc; } } static uint32_t crc32buf(unsigned char *buf, size_t len) { uint32_t crc = 0xFFFFFFFF; for (; len; len--, buf++) crc = crc32[(uint8_t)crc ^ *buf] ^ (crc >> BPB); return ~crc; } struct header { unsigned char model[16]; uint32_t crc; }; static void usage(const char *) __attribute__ (( __noreturn__ )); static void usage(const char *mess) { fprintf(stderr, "Error: %s\n", mess); fprintf(stderr, "Usage: add_header model_id input_file output_file\n"); fprintf(stderr, "\n"); exit(1); } int main(int argc, char **argv) { off_t len; // of original buf off_t buflen; // of the output file int fd; void *input_file; // pointer to the input file (mmmapped) struct header header; unsigned char *buf; // pointer to prefix + copy of original buf // verify parameters if (argc != 4) usage("wrong number of arguments"); // mmap input_file if ((fd = open(argv[2], O_RDONLY)) < 0 || (len = lseek(fd, 0, SEEK_END)) < 0 || (input_file = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0)) == (void *) (-1) || close(fd) < 0) { fprintf(stderr, "Error loading file %s: %s\n", argv[2], strerror(errno)); exit(1); } buflen = len + sizeof(header); init_crc32(); // copy model name into header strncpy(header.model, argv[1], sizeof(header.model)); header.crc = 0; // create a firmware image in memory and copy the input_file to it buf = malloc(buflen); memcpy(buf, &header, sizeof(header)); memcpy(&buf[sizeof(header)], input_file, len); // CRC of temporary header + buf header.crc = htonl(crc32buf(buf, buflen)); memcpy(buf, &header, sizeof(header)); // write the buf if ((fd = open(argv[3], O_CREAT|O_WRONLY|O_TRUNC,0644)) < 0 || write(fd, buf, buflen) != buflen || close(fd) < 0) { fprintf(stderr, "Error storing file %s: %s\n", argv[3], strerror(errno)); exit(2); } free(buf); munmap(input_file,len); return 0; } git.cgi/openwrt/upstream/tree/package/libs/libnl-tiny?id=d46ce9498cf562cc9f0212152dcc8eebb20753ce'>libnl-tiny/src/include/netlink/object.h
blob: 8aabe7bb12fd02b317cc302067b95a1d8dc7bc56 (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * netlink/object.c	Generic Cacheable Object
 *
 *	This library is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation version 2.1
 *	of the License.
 *
 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
 */

#ifndef NETLINK_OBJECT_H_
#define NETLINK_OBJECT_H_

#include <netlink/netlink.h>
#include <netlink/utils.h>
#include <netlink/object-api.h>

#ifdef __cplusplus
extern "C" {
#endif

#define NL_OBJ_MARK		1

struct nl_cache;
struct nl_object;
struct nl_object_ops;

struct nl_object
{
	NLHDR_COMMON
};

#define OBJ_CAST(ptr)		((struct nl_object *) (ptr))

/* General */
extern struct nl_object *	nl_object_alloc(struct nl_object_ops *);
extern void			nl_object_free(struct nl_object *);
extern struct nl_object *	nl_object_clone(struct nl_object *obj);

#ifdef disabled

extern int			nl_object_alloc_name(const char *,
						     struct nl_object **);
extern void			nl_object_dump(struct nl_object *,
					       struct nl_dump_params *);

extern uint32_t			nl_object_diff(struct nl_object *,
					       struct nl_object *);
extern int			nl_object_match_filter(struct nl_object *,
						       struct nl_object *);
extern int			nl_object_identical(struct nl_object *,
						    struct nl_object *);
extern char *			nl_object_attrs2str(struct nl_object *,
						    uint32_t attrs, char *buf,
						    size_t);
#endif
/**
 * Check whether this object is used by multiple users
 * @arg obj		object to check
 * @return true or false
 */
static inline int nl_object_shared(struct nl_object *obj)
{
	return obj->ce_refcnt > 1;
}


static inline void nl_object_get(struct nl_object *obj)
{
	obj->ce_refcnt++;
}

static inline void nl_object_put(struct nl_object *obj)
{
	if (!obj)
		return;

	obj->ce_refcnt--;

	if (obj->ce_refcnt <= 0)
		nl_object_free(obj);
}


/**
 * @name Marks
 * @{
 */

/**
 * Add mark to object
 * @arg obj		Object to mark
 */
static inline void nl_object_mark(struct nl_object *obj)
{
	obj->ce_flags |= NL_OBJ_MARK;
}

/**
 * Remove mark from object
 * @arg obj		Object to unmark
 */
static inline void nl_object_unmark(struct nl_object *obj)
{
	obj->ce_flags &= ~NL_OBJ_MARK;
}

/**
 * Return true if object is marked
 * @arg obj		Object to check
 * @return true if object is marked, otherwise false
 */
static inline int nl_object_is_marked(struct nl_object *obj)
{
	return (obj->ce_flags & NL_OBJ_MARK);
}

/** @} */

#ifdef disabled
/**
 * Return list of attributes present in an object
 * @arg obj		an object
 * @arg buf		destination buffer
 * @arg len		length of destination buffer
 *
 * @return destination buffer.
 */
static inline char *nl_object_attr_list(struct nl_object *obj, char *buf, size_t len)
{
	return nl_object_attrs2str(obj, obj->ce_mask, buf, len);
}
#endif

/**
 * @name Attributes
 * @{
 */

static inline int nl_object_get_refcnt(struct nl_object *obj)
{
	return obj->ce_refcnt;
}

static inline struct nl_cache *nl_object_get_cache(struct nl_object *obj)
{
	return obj->ce_cache;
}

static inline void *		nl_object_priv(struct nl_object *obj)
{
	return obj;
}


/** @} */


#ifdef __cplusplus
}
#endif

#endif