2012年04月17日
PARTNER-JetでU-bootを追いかける(3)
PARTNER-Jetのバックトレースウインドウの関数の行をダブルクリックすると、コードウインドウにその部分のソースコードが表示されます。(クリックで拡大表示)
common/main.c のmain_loopのところを、無関係の#ifdefを取り除くと以下のようになります。
for (;;) {
len = readline (CONFIG_SYS_PROMPT);
flag = 0; /* assume no special flags for now */
if (len > 0)
strcpy (lastcommand, console_buffer);
else if (len == 0)
flag |= CMD_FLAG_REPEAT;
if (len == -1)
puts ("<INTERRUPT>\n");
else
rc = run_command (lastcommand, flag);
if (rc <= 0) {
/* invalid command or not repeatable, forget it */
lastcommand[0] = 0;
}
}
ループの中で、readlineでコマンド列を読み取り、run_commandでそれを実行しています。
run_commandの中を追いかけていくと、以下のようになっています。
/* find macros in this token and replace them */
process_macros (token, finaltoken);
/* Extract arguments */
if ((argc = parse_line (finaltoken, argv)) == 0) {
rc = -1; /* no command at all */
continue;
}
/* Look up command in command table */
if ((cmdtp = find_cmd(argv[0])) == NULL) {
printf ("Unknown command '%s' - try 'help'\n", argv[0]);
rc = -1; /* give up after bad command */
continue;
}
/* found - check max args */
if (argc > cmdtp->maxargs) {
cmd_usage(cmdtp);
rc = -1;
continue;
}
/* OK - call function to do the command */
if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
rc = -1;
}
最後の関数ポインタ cmdtp->cmd を呼び出しているところが、各コマンドの実行のようです。
PARTNER-Jetでここにブレークポイントを設定し、Goしてからシリアルコンソールから
"help"と入力してみます。

ここで止まりました。ファンクションキーF8でステップインしてみます。

確かにこれがhelpコマンドの実装のようです。
/*
* Use puts() instead of printf() to avoid printf buffer overflow
* for long help messages
*/
int do_help (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
{
int i;
int rcode = 0;
...
U_BOOT_CMD(
help, CONFIG_SYS_MAXARGS, 1, do_help,
"print online help",
"[command ...]\n"
" - show help information (for 'command')\n"
"'help' prints online help for the monitor commands.\n\n"
"Without arguments, it prints a short usage message for all commands.\n\n"
"To get detailed help information for specific commands you can type\n"
"'help' with one or more command names as arguments.\n"
);
do_help を参照しているところを探すと、U_BOOT_CMDというマクロが見つかりました。
おそらくこれでコマンドを登録しているのでしょう。
ここまで調べてからソースコードを見ると理解が早いです。
次回はU-Bootの起動シーケンスを調べてみます。
