Contiki 3.x
shell-exec.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * A brief description of what this file is.
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 #include "contiki.h"
41 #include "shell-exec.h"
42 #include "loader/elfloader.h"
43 
44 #include <stdio.h>
45 #include <string.h>
46 
47 /*---------------------------------------------------------------------------*/
48 PROCESS(shell_exec_process, "exec");
49 SHELL_COMMAND(exec_command,
50  "exec",
51  "exec <filename>: load and execute the ELF file filename",
52  &shell_exec_process);
53 /*---------------------------------------------------------------------------*/
54 PROCESS_THREAD(shell_exec_process, ev, data)
55 {
56  char *name;
57  int fd;
58 
59  PROCESS_BEGIN();
60 
61  name = data;
62  if(name == NULL || strlen(name) == 0) {
63  shell_output_str(&exec_command,
64  "exec <file>: filename must be given", "");
65  PROCESS_EXIT();
66  }
67 
68  /* Kill any old processes. */
70  autostart_exit(elfloader_autostart_processes);
71  }
72 
73  fd = cfs_open(name, CFS_READ | CFS_WRITE);
74  if(fd < 0) {
75  shell_output_str(&exec_command,
76  "exec: could not open ", name);
77  } else {
78  int ret;
79  char *print, *symbol;
80 
81  ret = elfloader_load(fd);
82  cfs_close(fd);
83  symbol = "";
84 
85  switch(ret) {
86  case ELFLOADER_OK:
87  print = "OK";
88  break;
90  print = "Bad ELF header";
91  break;
93  print = "No symbol table";
94  break;
96  print = "No string table";
97  break;
98  case ELFLOADER_NO_TEXT:
99  print = "No text segment";
100  break;
102  print = "Symbol not found: ";
103  symbol = elfloader_unknown;
104  break;
106  print = "Segment not found: ";
107  symbol = elfloader_unknown;
108  break;
110  print = "No starting point";
111  break;
112  default:
113  print = "Unknown return code from the ELF loader (internal bug)";
114  break;
115  }
116  shell_output_str(&exec_command, print, symbol);
117 
118  if(ret == ELFLOADER_OK) {
119  int i;
120  for(i = 0; elfloader_autostart_processes[i] != NULL; ++i) {
121  shell_output_str(&exec_command, "exec: starting process ",
123  }
124  autostart_start(elfloader_autostart_processes);
125  }
126 
127  }
128 
129  PROCESS_END();
130 }
131 /*---------------------------------------------------------------------------*/
132 void
133 shell_exec_init(void)
134 {
135  elfloader_init();
136  shell_register_command(&exec_command);
137 }
138 /*---------------------------------------------------------------------------*/
#define ELFLOADER_NO_TEXT
Return value from elfloader_load() indicating that the size of the .text segment was zero...
Definition: elfloader.h:103
#define ELFLOADER_SEGMENT_NOT_FOUND
Return value from elfloader_load() indicating that one of the required segments (.data, .bss, or .text) could not be found.
Definition: elfloader.h:116
void elfloader_init(void)
elfloader initialization function.
Definition: elfloader.c:317
int cfs_open(const char *name, int flags)
Open a file.
Definition: cfs-coffee.c:996
void shell_output_str(struct shell_command *c, char *text1, const char *text2)
Output strings from a shell command.
Definition: shell.c:383
#define PROCESS_EXIT()
Exit the currently running process.
Definition: process.h:200
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define CFS_WRITE
Specify that cfs_open() should open a file for writing.
Definition: cfs.h:104
int elfloader_load(int fd)
Load and relocate an ELF file.
Definition: elfloader.c:339
#define NULL
The null pointer.
#define CFS_READ
Specify that cfs_open() should open a file for reading.
Definition: cfs.h:90
#define ELFLOADER_NO_STRTAB
Return value from elfloader_load() indicating that no string table could be found in the ELF file...
Definition: elfloader.h:98
#define ELFLOADER_NO_SYMTAB
Return value from elfloader_load() indicating that no symbol table could be found in the ELF file...
Definition: elfloader.h:93
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
cle_scratch elfloader_unknown
If elfloader_load() could not find a specific symbol, it is copied into this array.
Definition: tcp_loader2.c:105
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
void shell_register_command(struct shell_command *c)
Register a command with the shell.
Definition: shell.c:413
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
struct process *const * elfloader_autostart_processes
A pointer to the processes loaded with elfloader_load().
Definition: elfloader.c:136
#define ELFLOADER_BAD_ELF_HEADER
Return value from elfloader_load() indicating that the ELF file had a bad header. ...
Definition: elfloader.h:88
#define ELFLOADER_SYMBOL_NOT_FOUND
Return value from elfloader_load() indicating that a symbol specific symbol could not be found...
Definition: elfloader.h:111
#define ELFLOADER_OK
Return value from elfloader_load() indicating that loading worked.
Definition: elfloader.h:83
A brief description of what this file is.
#define ELFLOADER_NO_STARTPOINT
Return value from elfloader_load() indicating that no starting point could be found in the loaded mod...
Definition: elfloader.h:121
Header file for the Contiki ELF loader.
#define SHELL_COMMAND(name, command, description, process)
Define a shell command.
Definition: shell.h:219
void cfs_close(int fd)
Close an open file.
Definition: cfs-coffee.c:1032