/
init.c
219 lines (190 loc) · 5.01 KB
/
init.c
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* Init
*
* This routine is the initialization task for this test program.
* It is called from init_exec and has the responsibility for creating
* and starting the tasks that make up the test. If the time of day
* clock is required for the test, it should also be set to a known
* value by this function.
*
* Input parameters: NONE
*
* Output parameters: NONE
*
* COPYRIGHT (c) 1989-2009.
* On-Line Applications Research Corporation (OAR).
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.com/license/LICENSE.
*
* $Id: init.c,v 1.23 2009/08/12 20:50:37 joel Exp $
*/
#define CONFIGURE_INIT
#include "system.h"
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <rtems/error.h>
#include <rtems/shell.h>
#include <rtems/untar.h>
void writeFile(
const char *name,
int mode,
const char *contents
)
{
int sc;
sc = setuid(0);
if ( sc ) {
printf( "setuid %s failed: %s:\n", name, strerror(errno) );
}
rtems_shell_write_file( name, contents );
sc = chmod ( name, mode );
if ( sc ) {
printf( "chmod %s: %s:\n", name, strerror(errno) );
}
}
/*
** dynamic load demo command
*/
extern void demo(char *libname, char *symname);
int demo_command(int argc, char *argv[])
{
/*
** Loading elf file
*/
if (argc != 3)
{
printf("Command Usage: load lib symbol\n");
return(-1);
}
else
{
demo(argv[1], argv[2]);
}
return(0);
}
/*
** function to start the shell and add new commands.
*/
void rtems_add_local_cmds(void)
{
/*
** Add commands
*/
rtems_shell_add_cmd("dldemo","misc","dynamic object file load demo",demo_command);
}
#define writeScript( _name, _contents ) \
writeFile( _name, 0777, _contents )
extern int _binary_tarfile_start;
extern int _binary_tarfile_size;
#define TARFILE_START _binary_tarfile_start
#define TARFILE_SIZE _binary_tarfile_size
void fileio_start_shell(void)
{
int sc;
sc = mkdir("/scripts", 0777);
if ( sc ) {
printf( "mkdir /scripts: %s:\n", strerror(errno) );
}
sc = mkdir("/etc", 0777);
if ( sc ) {
printf( "mkdir /etc: %s:\n", strerror(errno) );
}
printf(
"Creating /etc/passwd and group with three useable accounts\n"
"root/pwd , test/pwd, rtems/NO PASSWORD\n"
);
writeFile(
"/etc/passwd",
0644,
"root:7QR4o148UPtb.:0:0:root::/:/bin/sh\n"
"rtems:*:1:1:RTEMS Application::/:/bin/sh\n"
"test:8Yy.AaxynxbLI:2:2:test account::/:/bin/sh\n"
"tty:!:3:3:tty owner::/:/bin/false\n"
);
writeFile(
"/etc/group",
0644,
"root:x:0:root\n"
"rtems:x:1:rtems\n"
"test:x:2:test\n"
"tty:x:3:tty\n"
);
writeScript(
"/scripts/js",
"#! joel\n"
"\n"
"date\n"
"echo Script successfully ran\n"
"date\n"
"stackuse\n"
);
writeScript(
"/scripts/j1",
"#! joel -s 20480 -t JESS\n"
"stackuse\n"
);
rtems_shell_write_file(
"/scripts/j2",
"echo j2 TEST FILE\n"
"echo j2 SHOULD BE non-executable AND\n"
"echo j2 DOES NOT have the magic first line\n"
);
printf("Populating Root file system from TAR file.\n");
Untar_FromMemory(
(unsigned char *)(&TARFILE_START),
(unsigned long)&TARFILE_SIZE);
printf("Adding Local Commands.\n");
rtems_add_local_cmds();
printf(" =========================\n");
printf(" starting shell\n");
printf(" =========================\n");
rtems_shell_init(
"SHLL", /* task_name */
RTEMS_MINIMUM_STACK_SIZE * 4, /* task_stacksize */
100, /* task_priority */
"/dev/console", /* devname */
false, /* forever */
true, /* wait */
rtems_shell_login_check /* login */
);
}
/*
* RTEMS Startup Task
*/
rtems_task
Init (rtems_task_argument ignored)
{
puts( "\n\n*** FILE I/O SAMPLE AND TEST ***" );
fileio_start_shell ();
}
/*
* RTEMS Shell Configuration -- Add a command and an alias for it
*/
int main_usercmd(int argc, char **argv)
{
int i;
printf( "UserCommand: argc=%d\n", argc );
for (i=0 ; i<argc ; i++ )
printf( "argv[%d]= %s\n", i, argv[i] );
return 0;
}
rtems_shell_cmd_t Shell_USERCMD_Command = {
"usercmd", /* name */
"usercmd n1 [n2 [n3...]] # echo arguments", /* usage */
"user", /* topic */
main_usercmd, /* command */
NULL, /* alias */
NULL /* next */
};
rtems_shell_alias_t Shell_USERECHO_Alias = {
"usercmd", /* command */
"userecho" /* alias */
};
#define CONFIGURE_SHELL_USER_COMMANDS &Shell_USERCMD_Command
#define CONFIGURE_SHELL_USER_ALIASES &Shell_USERECHO_Alias
#define CONFIGURE_SHELL_COMMANDS_INIT
#define CONFIGURE_SHELL_COMMANDS_ALL
#include <rtems/shellconfig.h>