Changed target for ubpar.c to directly write to mtd device
[openwrt-10.03/.git] / target / linux / at91-2.6 / image / u-boot / ubclient / ubpar.c
1 /*
2  * ubparams.c
3  * 
4  * Generate a u-boot parameter block with correct crc
5  *
6  * (C) 1007 Guthrie Consulting
7  * hamish@prodigi.ch
8  *
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #ifndef __ASSEMBLY__
16 #define __ASSEMBLY__
17 #endif
18 #define __ASM_STUB_PROCESSOR_H__
19 #include <config.h>
20 #undef __ASSEMBLY__
21 #include <environment.h>
22
23 #define XMK_STR(x)  #x
24 #define MK_STR(x)  XMK_STR(x)
25
26 extern unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
27
28 #if !defined(ENV_CRC)
29 #define ENV_CRC ~0
30 #endif
31
32 static char *environment[] = {
33         "bootdelay=3\0"
34         "baudrate=115200\0"
35         "stdin=serial\0"
36         "stdout=serial\0"
37         "stderr=serial\0"
38         "fbargs=setenv bootargs root=/dev/mtdblock3 ro init=/etc/preinit console=/dev/ttyS0,115200,mem=32M\0"
39         "rdba=setenv bootargs root=/dev/ram rw initrd=0x21200000,6000000 ramdisk_size=20000 init=/etc/preinit console=/dev/ttyS0,115200,mem=32M\0"
40         "rdram=run rdba; tftp 21000000 vImage; tftp 21200000 root.squashfs; bootm 21000000\0"
41         "flash=run fbargs; bootm 0xc0042000\0"
42         "bootargs=setenv bootargs root=/dev/mtdblock3 ro init=/etc/preinit console=/dev/ttyS0,115200,mem=32M\0"
43         "bootcmd=bootm 0xc0042000\0"
44         "ipaddr=10.0.1.73\0"
45         "serverip=10.0.1.210\0"
46         "\0"
47         };
48
49 int main(int argc, char *argv[]) {
50         env_t *envptr;
51         char *src, *srcptr;
52         char *dataptr;
53         FILE *params;
54         int argfail = 1;
55         char newmac[30];
56         char newser[30];
57         int paramlen = 0;
58         int progmac = 0;
59         int progser = 0;
60
61         if (argc < 3) {
62                 printf ("Invalid arguments\n");
63                 return 1;
64                 }
65
66         switch (argc) {
67                 case 5:
68                         if (strcmp(argv[3], "--serial") == 0) {
69                                 argfail = 0;
70                                 sprintf(newser, "serial#=%s", argv[4]);
71                                 progser = 1;
72                                 }
73                 case 3:
74                         if (strcmp(argv[1], "--mac") == 0) {
75                                 argfail = 0;
76                                 sprintf(newmac, "ethaddr=%s", argv[2]);
77                                 progmac = 1;
78                                 }
79                         else
80                                 argfail = 1;
81                 }
82
83         if (argfail) {
84                 printf("Invalid arguments\n");
85                 return 1;
86                 }
87
88
89         src = srcptr = *environment;
90         envptr = (env_t *)malloc(CFG_ENV_SIZE);
91         dataptr = (char *)envptr + ENV_HEADER_SIZE;
92
93         while(*srcptr) {
94                 //printf("%d, %s\n", strlen(srcptr), srcptr);
95                 paramlen += strlen(srcptr) + 1;
96                 srcptr += strlen(srcptr) + 1;
97                 }
98
99         printf("Make u-boot params\n");
100         printf("Params size is %d\n", CFG_ENV_SIZE);
101
102         memset(envptr, 0, CFG_ENV_SIZE);
103         memcpy(dataptr, src, paramlen);
104         dataptr += paramlen;
105
106         if (progmac) {
107                 memcpy(dataptr, newmac, strlen(newmac));
108                 dataptr += strlen(newmac) + 1;
109                 }
110
111         if (progser) {
112                 memcpy(dataptr, newser, strlen(newser));
113                 dataptr += strlen(newser) + 1;
114                 }
115
116         envptr->crc = crc32(0, envptr->data, ENV_SIZE);
117
118         params = fopen("/dev/mtd1", "w");
119         fwrite(envptr, CFG_ENV_SIZE, 1, params);
120         fclose(params);
121
122         free(envptr);
123         return 0;
124 }