龙芯俱乐部开源技术社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 10280|回复: 5

裸机编程之helloworld -PMON引导篇

[复制链接]

4

主题

8

帖子

673

积分

版主

Rank: 7Rank: 7Rank: 7

积分
673
发表于 2016-2-18 19:14:38 | 显示全部楼层 |阅读模式
本帖最后由 佐须之男 于 2017-11-28 16:18 编辑

众所周知,pmon可以引导elf程序,所以理论上不需要用汇编就可以编写裸机程序。

准备工作:
1.配置交叉编译工具,步骤略。
2.配置tftp server,步骤略。

背景知识介绍:
1.龙芯启动后的运行的代码位置是:0xbfc00000 (spi引导模式)。
2.pmon引导的地址是0x80200000(大部分bootloader都是此地址)。
3.gcc编译好的程序不能直接运行,因为内存映射不同,所以需要用ld工具重新映射地址空间。

教程部分:
1.从http://git.oschina.net/ForgotFun/loongson下载hello的代码,cd hello执行make,makefile会自动拷贝hello到/tftpboot目录
2.配置tftp server和设置本机的IP地址为 192.168.1.2
3.设置pmon的环境变量,ctrl+c 进入pmon的命令模式,执行
set syn0.ipaddr 192.168.1.1
set al tftp://192.168.1.2/a.out
假如出现env环境下,你的mac地址为IP地址是设置不了的,这种情况会出现在自己编译的pmon上,解决方案是执行
set ethaddr ea:8e:10:a6:ec:93
4.按主板上的复位键,运行程序,出现如下信息,说明成功了

Loading file: tftp://192.168.1.2/a.out (elf)
0x80200000/4112 + 0x80201010/4080(z) + 15 syms/
Entry address is 80200008
g root=/dev/hda1 console=tty
   zero      at       v0       v1       a0       a1       a2       a3   
00000000 00000000 00000000 00000000 00000003 a10ffd18 a10ffd28 8009b900
    t0       t1       t2       t3       t4       t5       t6       t7   
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
    s0       s1       s2       s3       s4       s5       s6       s7   
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
    t8       t9       k0       k1       gp       sp       s8       ra   
00000000 00000000 00000000 00000000 00000000 a10ffcf8 00000000 8005be30


Hello! This is the 'hello' program!

It was invoked with:
Arg  0: g
Arg  1: root=/dev/hda1
Arg  2: console=tty



自由职业嵌入式工程师(专业提供网络设备解决方案)
联系方式:18013582125,18114354589 (陆工)
个人博客:forgotfun.org

48

主题

257

帖子

4920

积分

新手上路

Rank: 1

积分
4920
发表于 2016-2-19 08:51:19 | 显示全部楼层
我觉得重点应该是怎么实现重映射的,下面把楼主git中的Makefile和ld.script部分内容贴上来

Makefile内容如下
CROSS_COMPILE         =mipsel-linux-

#
# Include the make variables (CC, etc...)
#

AS                = $(CROSS_COMPILE)as
LD                = $(CROSS_COMPILE)ld
CC                = $(CROSS_COMPILE)gcc
CPP                = $(CC) -E
AR                = $(CROSS_COMPILE)ar
NM                = $(CROSS_COMPILE)nm
STRIP                = $(CROSS_COMPILE)strip
OBJCOPY                = $(CROSS_COMPILE)objcopy
OBJDUMP                = $(CROSS_COMPILE)objdump
SIZE                = $(CROSS_COMPILE)size

hello:        hello.c
        ${CC} -G 0 -O2  -fno-pic -mno-abicalls -fno-builtin -nostdinc -mips3 -Ttext=0xffffffff80200000 -N -o hello hello.c -nostdlib -e main -Wl,-m -Wl,elf32ltsmip -T ld.script
        cp hello /tftpboot/a.out


ld.script内容如下
OUTPUT_FORMAT("elf32-tradlittlemips", "elf32-tradbigmips",
              "elf32-tradlittlemips")
OUTPUT_ARCH(mips)
ENTRY(_start)
SECTIONS
{
  . = 0xffffffff81000000;
  .text :
  {
    _ftext = . ;
    *(.text)
    *(.rodata)
    *(.rodata1)
    *(.reginfo)
    *(.init)
    *(.stub)
    *(.gnu.warning)
  } =0
  _etext = .;
  PROVIDE (etext = .);
  .fini : { *(.fini) } =0
  .data :
  {
    _fdata = . ;
    *(.data)
   . = ALIGN(32);
   *(.data.align32)
   . = ALIGN(64);
   *(.data.align64)
   . = ALIGN(128);
   *(.data.align128)
   . = ALIGN(4096);
   *(.data.align4096)
    CONSTRUCTORS
  }
  .data1 : { *(.data1) }
  .ctors :
  {
                __CTOR_LIST__ = .;
                LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)
               *(.ctors)
                LONG(0)
                __CTOR_END__ = .;
  }
  .dtors :
  {
                __DTOR_LIST__ = .;
                LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)
               *(.dtors)
                LONG(0)
                __DTOR_END__ = .;
  }
  _gp = ALIGN(16) + 0x7ff0;
  .got :
  {
    *(.got.plt) *(.got)
   }
  .sdata : { *(.sdata) }
  .lit8 : { *(.lit8) }
  .lit4 : { *(.lit4) }
  _edata = .;
  PROVIDE (edata = .);
  __bss_start = .;
  _fbss = .;
  .sbss : { *(.sbss) *(.scommon) }
  .bss :
  {
   *(.dynbss)
   *(.bss)
   . = ALIGN(32);
   *(.bss.align32)
   . = ALIGN(64);
   *(.bss.align64)
   . = ALIGN(128);
   *(.bss.align128)
   . = ALIGN(4096);
   *(.bss.align4096)
   *(COMMON)
  }
  _end = . ;
  PROVIDE (end = .);
  .stab 0 : { *(.stab) }
  .stabstr 0 : { *(.stabstr) }
  .debug 0 : { *(.debug) }
  .debug_srcinfo 0 : { *(.debug_srcinfo) }
  .debug_aranges 0 : { *(.debug_aranges) }
  .debug_pubnames 0 : { *(.debug_pubnames) }
  .debug_sfnames 0 : { *(.debug_sfnames) }
  .line 0 : { *(.line) }
  .gptab.sdata : { *(.gptab.data) *(.gptab.sdata) }
  .gptab.sbss : { *(.gptab.bss) *(.gptab.sbss) }
}

48

主题

257

帖子

4920

积分

新手上路

Rank: 1

积分
4920
发表于 2016-2-19 08:52:12 | 显示全部楼层
为楼主点赞!

0

主题

3

帖子

120

积分

注册会员

Rank: 2

积分
120
发表于 2016-2-19 10:59:31 | 显示全部楼层
为楼主点赞+1

84

主题

465

帖子

44万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
440441
发表于 2016-2-19 12:16:59 | 显示全部楼层
不错 有人来研究PMON了,很多初学者的福音啊。

0

主题

2

帖子

181

积分

注册会员

Rank: 2

积分
181
发表于 2017-6-7 23:16:26 | 显示全部楼层
顶楼主!!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|龙芯俱乐部开源技术社区

GMT+8, 2024-4-25 21:55 , Processed in 0.093250 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表